相关疑难解决方法(0)

C++ 1y/C++ 14:变量模板专业化?

根据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)

c++ templates c++14

33
推荐指数
2
解决办法
5396
查看次数

传递const char*作为模板参数

为什么你不能在这里传递文字字符串?我使用了一个非常轻微的解决方法.

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)

c++ string templates

28
推荐指数
4
解决办法
3万
查看次数

标签 统计

c++ ×2

templates ×2

c++14 ×1

string ×1