8

my_struct関数であるメンバー変数を囲む型fがあるとします。fが c++11 ラムダ関数になる可能性があります。

my_structラムダ オブジェクトへの代入は違法なので、 がラムダの場合は代入されないようにの代入演算子を実装したいと思いますf

is_lambda型のラムダ性を検査できる型特性を構築することは可能ですか?

コード内:

#include <type_traits>

template<typename Function> struct is_lambda
{
  // what goes here?
};

template<typename Function> struct my_struct
{
  Function f;

  my_struct &do_assign(const my_struct &other, std::true_type)
  {
    // don't assign to f
    return *this;
  }

  my_struct &do_assign(const my_struct &other, std::false_type)
  {
    // do assign to f
    f = other.f;
    return *this;
  }

  my_struct &operator=(const my_struct &other)
  {
    return do_assign(other, typename is_lambda<Function>::type());
  }
};
4

2 に答える 2

10

ラムダの型は通常の非共用体クラス型であるため、コンパイラのサポートなしでは不可能です。

§5.1.2 [expr.prim.lambda] p3

ラムダ式の型(クロージャー オブジェクトの型でもあります) は、一意の名前のない非共用体クラス型です [...]

于 2011-12-12T22:46:22.170 に答える
6

おそらく、割り当て不可能な非ラムダ関数も割り当てたくないので、を使用できますstd::is_assignable

于 2011-12-12T22:51:35.920 に答える