Che*_*Alf 8 c++ lambda constructor
更重要的是,这段代码出了什么问题:
#include <assert.h>
#include <functional>
using namespace std;
template< class BaseObjectId >
class Check
{
protected:
Check( function<bool()> const f ) { assert( f() ); }
};
template< int tpMinValue, int tpMaxValue >
class IntegerSubrange
: private Check< IntegerSubrange< tpMinValue, tpMaxValue > >
{
private:
int value_;
public:
enum :int { minValue = tpMinValue, maxValue = tpMaxValue };
static bool rangeContains( int const x )
{
return (minValue <= x && x <= maxValue);
}
operator int() const
{
return value_;
}
void operator/=( int const rhs )
{
value_ /= rhs;
assert( rangeContains( value_ ) );
}
explicit IntegerSubrange( int const value )
: Check< IntegerSubrange< tpMinValue, tpMaxValue > >(
[=]() -> bool { return rangeContains( value ); }
)
, value_( value )
{}
};
int main() {}
Run Code Online (Sandbox Code Playgroud)
Visual C++报告语法错误:
foo.cpp foo.cpp(41) : error C2059: syntax error : ')' foo.cpp(44) : see reference to class template instantiation 'IntegerSubrange' being compiled foo.cpp(42) : error C2059: syntax error : ',' foo.cpp(43) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
总结一下评论:提问者的代码是有效的。显然,一些早于 GCC 4.4 或 Visual C++ 2011 的编译器会拒绝它,因为这些编译器对 C++11 样式 lambda 的支持不完整。但现代编译器(当然还有任何声称支持新 C++11 标准的编译器)应该可以很好地处理它。
\n\n从字面上回答你的问题:在ctor-initializer-list中,可以使用相同的标识符(并指代相同的事物),就像将它们移动到构造函数本身的大括号内时所指的一样。特别是,这意味着您可以这样做
\n\nclass C {\n const char *p_ = "foo";\n char c_;\n C(int): p_(__func__) { } // referring to "__func__"\n C(double): c_(*this->p_) { } // referring to "this"\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n以下是该标准关于该主题的规定:
\n\n\n\nmem 初始值设定项的表达式列表或花括号初始化列表中的名称在指定了mem 初始值设定项的构造函数的范围内进行计算 。... [注意:因为mem 初始化器是在构造函数的范围内计算的,所以可以在mem 初始化器的表达式列表中使用指针来引用正在初始化的对象。已初始化。\xe2\x80\x94尾注] (N3337 \xc2\xa712.6.2 #12)
\nthis