我正在尝试为hash<>
我一直在研究的一系列类型提供专业化。到目前为止一切顺利,专业化本身很容易提供,我已经为numeric_limits<>
. 但我面临的问题是如何以可移植到 C++11 和 -pre-11(C++03 或其他)的方式提供专业化。
当然,我遇到的问题是hash<>
可以在几个命名空间之一中定义,并且我被迫在同一个命名空间中提供专业化。
// in c++11
namespace std { struct hash<>...; }
// in c++03 this is one possibility
namespace std { namespace tr1 { struct hash<>...; } }
// my library's specialization
namespace ????_open {
struct hash<mytype>...;
????_close
当然,一种选择是使用#defines 来访问、打开和关闭足够的命名空间,或者提供具有N 个不同专业化的N 个文件并有条件地#include 正确的一个,但这很麻烦:
#if defined(some_c++11_condition)
#include "c++11-specialization.h"
#elif defined(some_c++03_condition)
#include "c++03-specialization.h"
#elif (some_other_condition)
#oh_dear_who_knows_what_this_include_will_be_like
#else_ad_nauseam
#endif
当然,如果我被迫这样做,我会坚持这个策略,但我之前想探索其他一些选择。特别是,我虽然可以使用命名空间别名来专注于正确的地方:
#if defined(some_c++11_condition)
namespace std_specialize = std;
#elif defined(some_c++03_condition)
namespace std_specialize = std::tr1;
#...
#endif
...
namespace std_specialize {
struct hash<mytype>...;
}
不幸的是,这在我尝试过的 3 个编译器(MSVC 2008、GCC 4.7、Clang 3.0)中的任何一个中都不起作用,并且declaration of namespace conflicts with...
在重新打开命名空间的行中出现了关于“”的各种错误,这不应该发生,因为命名空间可以重新打开多个次,如果别名是别名而不是其他东西,那么这也应该适用于他们。
那么,命名空间别名真的是别名,还是误称有其他含义?还是有其他原因我不能以这种方式专业化?如果是这样,还有其他方法(比#defines 的弹幕更好)吗?