我有一些lambda函数,我想用boost :: bind或std :: bind绑定.(不管哪一个,只要它有效.)不幸的是它们都给了我不同的编译器错误:
auto f = [](){ cout<<"f()"<<endl; };
auto f2 = [](int x){ cout<<"f2() x="<<x<<endl; };
std::bind(f)(); //ok
std::bind(f2, 13)(); //error C2903: 'result' : symbol is neither a class template nor a function template
boost::bind(f)(); //error C2039: 'result_type' : is not a member of '`anonymous-namespace'::<lambda0>'
boost::bind(f2, 13)(); //error C2039: 'result_type' : is not a member of '`anonymous-namespace'::<lambda1>'
Run Code Online (Sandbox Code Playgroud)
那么,最简单的解决方法是什么?
假设我有一个可以保存基类方法地址的指针类型.我可以为其分配子类方法的地址并期望它正常工作吗?在我的情况下,我使用它与基类指针,对象的动态类型是派生类.
struct B
{
typedef void (B::*MethodPtr)();
};
struct D: public B
{
void foo() { cout<<"foo"<<endl; }
};
int main(int argc, char* argv[])
{
D d;
B* pb = &d;
//is the following ok, or undefined behavior?
B::MethodPtr mp = static_cast<B::MethodPtr>(&D::foo);
(pb->*mp)();
}
Run Code Online (Sandbox Code Playgroud)
在讨论static_cast时,标准说明了这一点:
5.2.9.9类型"指向Cv1 T类型D的成员的指针"的rvalue可以转换为类型为"指向cv2 T类型B的成员的指针"的rvalue,其中B是D的基类(第10节),如果a从"指向T类型B的成员的指针"到"指向T类型D的成员的指针"的有效标准转换存在(4.11),并且cv2与cv1具有相同的cv资格,或者更高的cv资格.63)空成员指针值(4.11)被转换为目标类型的空成员指针值.如果类B包含原始成员,或者是包含原始成员的类的基类或派生类,则指向成员的结果指针指向原始成员.否则,演员的结果是不确定的.[注意:虽然B类不需要包含原始成员,取消引用成员指针的对象的动态类型必须包含原始成员; 见5.5.]
和往常一样,我很难破译标准.它有点说没关系,但我不能100%确定上述文本是否真的适用于我的示例代码中的情况.
在这两种语言的基本来源字符集包括每除外打印的ASCII字符@,$和`.我可以理解不使用重音,因为它并不总是被解释为一个单独的角色,它看起来也非常类似于撇号.但有没有一个特定的原因,为什么@和$没有任何用法或语言设计师只是用尽了想法?:)
这是我能想出的最简单的例子来重现问题.
template<class T>
struct X
{
static void foo()
{
static int z = 0;
[]{ z = 1; }();
}
};
int main()
{
X<int>::foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用MinGW 4.6和4.7,以及Ubuntu中的g ++ 4.6,所有这些都给我链接错误"undefined reference to z".所以现在这让我想知道这是否合法.VC10没问题.
如果X是普通类而不是模板,它可以工作.另外,我认为它与lambdas无关,因为即使我用本地类替换lambda也会出错.
更具体地说,假设我有一个带参数的类模板A和B,我想有一个编译器错误(当模板被实例化)如果B未从A派生
template<class A, class B>
class Foo
{
// static_assert(B is derived from A)
};
Run Code Online (Sandbox Code Playgroud) 在VC++ 10中,以下示例因错误C2027而失败:"使用未定义类型'X'".但是g ++ 4.6编译得很好.
template<class T>
class C
{
static const size_t size = sizeof(T);
};
class X : public C<X> { };
Run Code Online (Sandbox Code Playgroud)
那么哪一个是对的?我将如何做到这一点,以便它适用于主流编译器?
这并不是什么大不了的事,因为VC++仍然允许在C的成员函数中使用sizeof(T).我只需要重复一些烦人的长类型定义.
编辑: 我意识到我的例子很糟糕,因为我真正想做的是将大小用作编译时常量,这样:
template<size_t size> class C2 { };
template<class T>
class C
{
typedef C2<sizeof(T)> A;
};
class X : public C<X> { };
Run Code Online (Sandbox Code Playgroud)
两个编译器都拒绝这个,所以我认为它可能不可能,但就像我说我仍然可以在函数内部使用sizeof.我只是希望我不必在每个函数中重复typedef.
template<size_t size> class C2 { };
template<class T>
class C
{
void foo() { typedef C2<sizeof(T)> A; }
};
class X : public C<X> { };
Run Code Online (Sandbox Code Playgroud) 我正在使用ifstream在C++中实现一个必须在大文件中搜索的程序(~1TB).但是,读取2GB后失败.有没有办法获得文件位置,即使对于大文件?我编译为32位Windows机器.
std::ifstream f;
f.open( filename.c_str(), std::ifstream::in | std::ifstream::binary );
while(true) {
std::cout << (uint64_t)(f.tellg()) << std::endl;
//read data
}
Run Code Online (Sandbox Code Playgroud) 我想要强大的枚举类型.C++ 0x具有此功能,但不幸的是它们还需要显式范围:
enum class E {e1, e2, e3};
E x = E::e1; //OK
E y = e1; //error
Run Code Online (Sandbox Code Playgroud)
有时这是可取的,但有时它只是不必要地冗长.标识符本身可能足够唯一,或者枚举可能已经嵌套在类或命名空间中.
所以我正在寻找一种解决方法.在周围范围内声明枚举值的最佳方法是什么?