是否可以在“现代 C++”(C++17 或更高版本)中将字符串文字作为参数传递给 C++ 模板?
我意识到你可以用构造函数参数来做到这一点;我只是认为将它作为模板参数会更方便,而不是深埋在 cpp 文件中。我很好奇这是否是现代 C++ 的一个新特性。请参阅下面的伪代码,了解我正在尝试做的事情:
伪代码示例:
// Header File /////////////////////////
template<constexpr string Name>
class ModuleBase {
public:
ModuleBase();
string name;
};
class xyz : ModuleBase<"xyz"> {
public:
xyz();
};
// Cpp File //////////////////////////
template<string_literal Name>
ModuleBase<Name>::ModuleBase() {
name = Name;
}
xyz::xyz() : ModuleBase() {
}