有没有一种方法可以从变量中设置模板参数
Is there a way to set a template parameter from a variable?
我有一个枚举值作为类的成员,我想把它作为模板参数传递?编译器抱怨不能在常量表达式中使用该成员。有什么魔法可以完成这项工作吗?
我当前的解决方案是一个switchcase语句,但在我的原始代码EType
中有近200个条目。因此,我最初的想法是编写一个type_traits,用于将枚举值映射到类型。
下面是我的问题的一个例子(也在ideone.com上)(问题是main()
中的最后一行):
#include <iostream>
enum EType
{
eType_A,
eType_B,
eType_C
};
struct Foo
{
Foo(EType eType)
: m_eType(eType)
{
}
EType m_eType;
};
template <EType eType>
struct Bar
{
static std::string const toString()
{
return "-";
}
};
template <>
struct Bar<eType_A>
{
static std::string const toString()
{
return "A";
}
};
template <>
struct Bar<eType_B>
{
static std::string const toString()
{
return "B";
}
};
int main(int argc, char *argv[])
{
std::cout << "Bar<eType_A>::toString()=" << Bar<eType_A>::toString() << "n";
std::cout << "Bar<eType_B>::toString()=" << Bar<eType_B>::toString() << "n";
std::cout << "Bar<eType_C>::toString()=" << Bar<eType_C>::toString() << "n";
Foo stFooA(eType_A);
std::cout << "Bar<stFooA.m_eType>::toString()=" << Bar<stFooA.m_eType>::toString() << "n"; // <--- here ist the problem
}
示例生成以下错误:
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:54: error: ‘stFooA’ cannot appear in a constant-expression
prog.cpp:54: error: `.' cannot appear in a constant-expression
prog.cpp:54: error: template argument 1 is invalid
传递到Bar的模板参数在编译时必须是已知的。您会得到这个错误,因为stFooA.m_eType
可能在运行时发生更改,因此无法工作。
为了回答你的另一个问题,让它发挥作用有什么魔力吗?也许——您允许在编译时公开m_eType
的值吗?如果这对你的问题是一个有效的约束,你可以把它改成这样,它就会起作用:
// ...
template <EType eType>
struct Foo
{
Foo()
{
}
static const EType m_eType = eType;
};
int main(int argc, char *argv[])
{
std::cout << "Bar<eType_A>::toString()=" << Bar<eType_A>::toString() << "n";
std::cout << "Bar<eType_B>::toString()=" << Bar<eType_B>::toString() << "n";
std::cout << "Bar<eType_C>::toString()=" << Bar<eType_C>::toString() << "n";
Foo<eType_A> stFooA;
std::cout << "Bar<stFooA.m_eType>::toString()="
<< Bar<Foo<eType_A>::m_eType>::toString()
<< "n"; // Foo<eType_A>::m_eType is known at compile-time so this works
}
否,模板扩展是在编译时完成的。只有在编译程序时知道该值时,它才起作用。
编译器还要求一个常量表达式。
相关文章:
- 将双精度变量设置为另一个变量的值
- 将变量设置为类型模板
- 如何为具有整数名称的变量设置位集
- 在 c++ 中将多个变量设置为相同的值
- 我是否必须在类中的所有变量C++设置为 private?
- 在C 中,如何使用变量设置Char Grid2d [x] [y]
- 如果将变量设置为等于新对象,旧对象会发生什么情况?
- C++ 给出编译器错误,将一个(静态)常量全局变量设置为另一个静态常量变量
- 是否可以将类成员变量设置为类函数的结果?
- 将 c++ 代码中的当前用户环境变量设置为对其他进程(如 cmds)可见
- 此CMAKE错误意味着什么?我的变量设置为未发现
- 如何基于用户设置的初始变量设置一组常数值
- 如何使用const成员变量设置数组大小
- 使用变量设置数组长度
- 使用变量设置bitset的大小
- 在C 中,如何将派生类中的私有变量设置为基类的输入值
- C 标量变量设置为零值
- C 定义变量设置值
- 正在将类级别的2D数组变量设置为传入值-C++
- 是否有C++标准类在作用域出口处将变量设置为值