考虑以下几点:公共接入
template<int T>
class Test {
public:
constexpr static int A = T;
};
int main(int argsc, char** args) {
std::cout << Test<2>::T << std::endl; // Option 1
std::cout << Test<2>::A << std::endl; // Option 2
}
为什么不选择1个编译?看来static constexpr A
只是一个额外的步骤。 T
是否不公开?
有没有比上面创建可公开访问的成员A
更清洁的方式获得T
?
您不能在类之外访问模板参数。 – plasmacel
@plasmacel - 为什么这是真的?创建'A'的步骤似乎没有必要。 – Jack
您也可以创建一个允许检索模板参数的特征。 'template TestTrait;模板 TestTrait > struct {constexpr static int value = N; };' –
Jarod42