【发布时间】:2020-12-12 03:03:32
【问题描述】:
我想要实现的是使用一个结构作为另一个结构的模板参数,而不需要实例化,这样这一切都发生在编译时。例如:
template<int v>
struct Container {
static const int value = v;
};
template<Container a, Container b>
struct BiggerContainer {
static const int value = a.v > b.v ? a.v : b.v;
};
int main() {
std::cout << BiggerContainer<Container<42>, Container<42>>::value << std::endl;
return 0;
}
我使用此代码得到的错误如下:
./code.cpp: In function 'int main()':
./code.cpp:44:61: error: type/value mismatch at argument 1 in template parameter list for 'template<Container<...auto...> a, Container<...auto...> b> struct BiggerContainer'
44 | std::cout << BiggerContainer<Container<42>, Container<42>>::value << std::endl;
| ^~
./code.cpp:44:61: note: expected a constant of type 'Container<...auto...>', got 'Container<42>'
./code.cpp:44:61: error: type/value mismatch at argument 2 in template parameter list for 'template<Container<...auto...> a, Container<...auto...> b> struct BiggerContainer'
./code.cpp:44:61: note: expected a constant of type 'Container<...auto...>', got 'Container<42>'
我在这里做错了什么?这个错误到底是什么意思我可以改变什么来得到我正在尝试的东西?
谢谢!
【问题讨论】:
标签: c++ templates struct static constants