我有这样的结构:
struct A {
void i(int i) {}
void s(string const &s) {}
};
Run Code Online (Sandbox Code Playgroud)
现在当我尝试这个:
bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");
Run Code Online (Sandbox Code Playgroud)
第一行编译好,但第二行生成错误:
c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(299): error C2535: 'void std::binder1st<_Fn2>::operator ()(const std::basic_string<_Elem,_Traits,_Ax> &) const' : member function already defined or declared
with
[
_Fn2=std::mem_fun1_t<void,A,const std::string &>,
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional(293) : see declaration of 'std::binder1st<_Fn2>::operator ()'
with
[
_Fn2=std::mem_fun1_t<void,A,const std::string &>
]
c:\work\sources\exception\test\exception\main.cpp(33) : see reference to class template instantiation 'std::binder1st<_Fn2>' being compiled
with …Run Code Online (Sandbox Code Playgroud) 我正在开发一款游戏并且有一个有趣的问题.我有一些游戏范围的常量值,我想在一个文件中实现.现在我有这样的事情:
constants.cpp
extern const int BEGINNING_HEALTH = 10;
extern const int BEGINNING_MANA = 5;
Run Code Online (Sandbox Code Playgroud)
constants.hpp
extern const int BEGINNING_HEALTH;
extern const int BEGINNING_MANA;
Run Code Online (Sandbox Code Playgroud)
然后文件只是#include"constants.hpp"这很好用,直到我需要使用其中一个常量作为模板参数,因为外部链接的常量不是有效的模板参数.所以我的问题是,实现这些常量的最佳方法是什么?我担心简单地将常量放在头文件中会导致在每个翻译单元中定义它们.而且我不想使用宏.
谢谢
我问自己为什么以下代码有效以及说明符extern在实例化时的作用baz_instance:
struct baz {
int value;
};
extern const baz baz_instance = {3};
template<baz const& b>
int foo(){
return b.value;
}
int main(){
foo<baz_instance>();
return 1;
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码首先编译,如果extern省略说明符,为什么不编译?什么是extern符在这个例子吗?