【发布时间】:2017-10-31 15:23:10
【问题描述】:
我有一个带有两个模板参数 (MyCollection) 的模板和另一个模板 (TCTools) 期望一个带有一个模板参数作为模板参数的模板。
我定义了一个“桥”(TypedCollection)来获取一个模板,该模板带有一个来自 MyCollection 的参数和一个参数作为它的第一个参数,目的是将其传递给模板模板。
这很好用,如果我使用带有固定类型的桥作为参数,但是从另一个模板调用它并使用另一个模板的参数将无法编译。
#include <iostream>
using std::size_t;
template <class Scalar, size_t size>
struct MyCollection
{
MyCollection()
{
std::cout << "Made collection"
<< std::endl
<< " " << __PRETTY_FUNCTION__
<< std::endl;
}
};
template <class Scalar>
struct TypedCollection
{
template <size_t size>
using value = MyCollection<Scalar, size>;
};
template <template <size_t> class TC>
struct TCTools
{
static TC<10> *make_10_sized()
{
return new TC<10>();
}
};
template <class S>
void test()
{
// Will not compile
TCTools<TypedCollection<S>::value>::make_10_sized();
}
int main()
{
// works
TCTools<TypedCollection<int>::value>::make_10_sized();
test<int>();
return 0;
}
GCC 给出以下注释:
expected a class template, got ‘TypedCollection<S>::value’
整件事让我很困惑。为什么 test() 中的调用没有编译,而 main() 中的调用按预期工作?是否可以进行测试?
【问题讨论】:
标签: c++ c++11 templates template-templates