总是更喜欢[std :: bind(& A :: mem_fn,& obj)]到[std :: bind(& A :: mem_fn,obj)]?

时间:2016-12-30 06:09:06

标签: c++ c++11 design-patterns bind standards

<div class="radio">

这个例子好像说:

您应该始终 for loop <?php ob_start(); session_start(); require ("../include/db.class.php"); require ("../include/config.php"); require ("../include/function.php"); if($_SESSION['id'] != ''){ $sql_user = "SELECT * FROM `users` WHERE id=:id"; $data_user[id] = $_SESSION['id']; $result_user = $pdo->pdoGetRow($sql_user, $data_user); $user_id = $result_user['id']; $user_paid = $result_user['paid']; $user_full_name = $result_user['first_name']." ".$result_user['middle_name']." ".$result_user['last_name']; } else { header('Location: login.php'); } $start_time=$_POST['start_time']; $training_id=$_POST['training_id']; $questions_id=$_POST['questions_id']; $question_text=$_POST['question_text']; $db['db_host']="localhost"; $db['db_user']="root"; $db['db_pass']=""; $db['db_name']="zadli"; foreach ($db as $key => $value){ define(strtoupper($key), $value); } $con=mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); if($con){ mysqli_query($con,'SET NAMES "utf8";'); // echo "<h1>we are connected</h1>"; } else { die("eroor conection".mysqli_error($con)); } ?> <div class="form-group"> <table border="0"> <tr> <td colspan="2">Succedd !!!</td> </tr> <tr> <td><strong>First Name:</strong> </td> <td><?php echo $training_id ?></td> </tr> <tr> <td><strong>Last Name:</strong> </td> <td><?php echo $questions_id ?></td> </tr> <tr> <td><strong>Last Name:</strong> </td> <td><?php echo $x ?></td> </tr> </table> <? $queryq = "SELECT * FROM `training_questions` WHERE id=$questions_id"; $query_chek= mysqli_query($con, $queryq); $arrLEtter = ['أ', 'ب', 'ج', 'د']; while ($row = mysqli_fetch_assoc($query_chek)) { echo '<h4>'.$row['question_text'].'</h4>'; ?> <input name="questions_id" type="hidden" id="questions_id" value="<?= $row['id'] ?>"> <input name="training_id" type="hidden" id="training_id" value="<?= $training_id ?>"> <?php for($iCnt = 1; $iCnt <= 4; $iCnt++){ ?> <div class="radio"> <input name="option" type="radio" class="styled" id="option<?=$iCnt?>" value="<?=$iCnt?>"> <label for="option<?=$iCnt?>"> <strong><?=$arrLEtter[$iCnt -1]?> -</strong> <?= $row['option'.$iCnt] ?> </label> </div> <? } ?> <? } ?> </div>

更严重的是,如果复制#include <functional> struct A { A() = default; A(const A&) = delete; A& operator =(const A&) = delete; void foo() const {} }; int main() { A a; std::bind(&A::foo, &a); // ok std::bind(&A::foo, a); // error return 0; } 的对象代价高昂,则应避免使用后者。

我无法想出后者更好的情况。所以,我只是想知道:

为什么C ++标准禁止后者?

1 个答案:

答案 0 :(得分:0)

这取决于功能应该做什么:

  • 是否需要引用a

    std::bind(&A::foo, &a);
    

    该函数采用a的引用,因此应在a的范围内调用它。如果a成为我们的范围,则引用变为无效,并且调用该函数为UB。

  • 是否需要a的副本:

    std::bind(&A::foo, a);
    

    该函数复制a,因此可以在其他地方调用。

因此,如果您想要将该功能从该范围中传出并执行该功能,则必须通过a的副本。