lifeizer_list返回的生命周期扩展

时间:2016-06-06 11:19:51

标签: c++ lambda return initializer-list auto

所以我有一个lambda,其返回类型为 <?php $page_number = $_GET['page']; $dbdit = new PDO('mysql:host=localhost;dbname=laundry','root',''); $results = $dbdit->prepare("SELECT COUNT(*) FROM konsumen"); $results->execute(); $get_total_rows = $results->fetch(); $results = $dbdit->prepare("SELECT * FROM konsumen ORDER BY kode_pemesanan ASC LIMIT $page_number"); $resultkaryawan = $dbdit->prepare("SELECT * FROM karyawan"); $results->execute(); $resultkaryawan->execute(); $row = $results->fetch(); while($data = $resultkaryawan->fetch()){ print_r("<table align='center' > <form method='POST' action='save.php' align='center'> <tr> <td>Kode Pemesanan</td> <td>:</td> <td><input type='text' name='kode_pemesanan' value='".$row['kode_pemesanan']."' class='inputnomor' disabled></td> <tr> <td>Atas Nama</td> <td>:</td> <td><input type='text' name='atas_nama' value='".$row['atas_nama']."' class='inputnomor' disabled></td> <tr> <td>Jumlah Baju</td> <td>:</td> <td><input name='jumlahBaju' type='number' class='inputpemesanan' id='jumlahbaju' value='".$row['jmlhbaju']."' min='0' onChange='calcBaju()'> Rp.3500,00-/pcs <input name='totalBaju' type='number' class ='total' id='totalbaju' value='".$row['jmlhbaju'] * 3500 . "' min='0' disabled onChange='calcBaju()'></td> <tr> <td>Jumlah Celana</td> <td>:</td> <td><input name='jumlahCelana' type='number' class='inputpemesanan' id='jumlahCelana' value='".$row['jmlhcelana']."' min='0' onChange='calcCelana()'> Rp.2500,00-/pcs <input name='totalCelana' type='number' class ='total' id='totalcelana' value='".$row['jmlhcelana'] * 2500 . "' min='0' disabled onChange='calcCelana()'></td> <tr> <td>Jumlah Jaket</td> <td>:</td> <td><input name='jumlahJaket' type='number' class='inputpemesanan' id='jumlahjaket' value='".$row['jmlhjaket']."' min='0' onChange='calcJaket()'> Rp.6500,00-/pcs <input name='totalJaket' type='number' class ='total' id='totaljaket' value='".$row['jmlhjaket'] * 6500 . "' min='0' disabled onChange='calcJaket()'></td> <tr> <td>Jumlah Jas</td> <td>:</td> <td><input name='jumlahJas' type='number' class='inputpemesanan' id='jumlahjas' value='".$row['jmlhjas']."' min='0' onChange='calcJas()'> Rp.8500,00-/pcs <input name='totalJas' type='number' class ='total' id='totaljas' value='".$row['jmlhjas'] * 8500 . "' min='0' disabled onChange='calcJas()'></td> <tr> <td>Jumlah Dress</td> <td>:</td> <td><input name='jumlahDress' type='number' class='inputpemesanan' id='jumlahdress' value='".$row['jmlhdress']."' min='0' onChange='calcDress()'> Rp.7500,00-/pcs <input name='totalDress' type='number' class ='total' id='totaldress' value='".$row['jmlhdress'] * 7500 . "' min='0' disabled onChange='calcDress()'></td> <tr> <td>Status Pesanan</td> <td>:</td> <td><input type='radio' name='status_pesanan' value='sedangproses'>Sedang di Proses<input type='radio' name='status_pesanan' value='sudahselesai'>Sudah Selesai </td> <tr> <td>Pengurus Pesanan</td> <td>:</td> <td><select> <option value=''>>-----SELECT-----<</option> <option value=''>". $data['nm_karyawan'] ."</option> </select> </td> </form> </table> "); } ?> ,我遇到了auto在此处被销毁的阵列支持问题:

initializer_list

我会像这样使用lambda:

const auto foo = [](const auto& a, const auto& b, const auto& c) { return {a, b, c}; };

我正在工作的工作是他们的编码标准的一部分,所有lambda都是单一陈述(随意表达你的愤慨。)我认为我可以解决这个问题:

  1. auto bar = foo(1, 2, 3); for(const auto& i : bar) cout << i << endl; 一个foo的返回类型,但这会弄乱它的通用性:vector int
  2. 只需编写一个构造const auto foo = [](const auto& a, const auto& b, const auto& c) -> vector<int> { return {a, b, c}; }的模板化函数并将其返回:vector<T>
  3. 是否有可能将这些变量强制转换为一个容器,谁的支持不会在一行中被销毁,这样我就能保持lambda的template <typename T> vector<T> foo(const T& a, const T& b, const T& c){ return {a, b, c}; }返回类型?

1 个答案:

答案 0 :(得分:3)

库基础知识TS v2有std::experimental::make_array,这肯定会满足您的要求:

#include <experimental/array>
const auto foo = [](const auto& a, const auto& b, const auto& c) {
    return std::experimental::make_array(a, b, c); };

更一般地说,template argument deduction for constructors允许你写:

const auto foo = [](const auto& a, const auto& b, const auto& c) {
    return std::vector{a, b, c}; };
                      ^-- no template parameter required

今天,您可以使用common_type

来模拟这一点
const auto foo = [](const auto& a, const auto& b, const auto& c) {
    return std::vector<std::common_type_t<decltype(a), decltype(b), decltype(c)>>{a, b, c}; };