hơn vào vấn đề, có gì sai với mã này:mà định danh có sẵn để lambda trong constructor danh sách initializer
#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() {}
Visual C++ báo cáo một lỗi cú pháp:
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
Biên dịch tốt với g ++ 4.6.3 –
ah, tôi cần phải cập nhật g của tôi ++ sau đó. tôi cũng sẽ kiểm tra bằng C++ 11 trực quan! ** Nó biên dịch tốt với Visual C++ 11 **, tôi sẽ không tin nó –
@Griwes: g ++ 4.6.3 mới hơn 4.6.1 và 4.6.1 biên dịch lambdas tốt. –