根据C++ 1y/C++ 14 N3690,变量模板特化的类型是否必须与主模板的类型相同?
template<int x>
char y = f(x);
template<>
double y<42> = g();
Run Code Online (Sandbox Code Playgroud)
如果是这样,是否有可能以某种方式保留主要的未定义?
template<int x>
???? y = ???; // undefined
template<>
double y<42> = g();
Run Code Online (Sandbox Code Playgroud)
草案涵盖哪些内容?
类模板的等效功能是:
template<int x>
struct S
{
static char y;
};
template<>
struct S<42>
{
static double y;
};
Run Code Online (Sandbox Code Playgroud)
和
template<int x>
struct S; // undefined
template<>
struct S<42>
{
static double y;
};
Run Code Online (Sandbox Code Playgroud) 为什么你不能在这里传递文字字符串?我使用了一个非常轻微的解决方法.
template<const char* ptr> struct lols {
lols() : i(ptr) {}
std::string i;
};
class file {
public:
static const char arg[];
};
decltype(file::arg) file::arg = __FILE__;
// Getting the right type declaration for this was irritating, so I C++0xed it.
int main() {
// lols<__FILE__> hi;
// Error: A template argument may not reference a non-external entity
lols<file::arg> hi; // Perfectly legal
std::cout << hi.i;
std::cin.ignore();
std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)