【发布时间】:2020-05-31 20:34:05
【问题描述】:
我想从一个 typedef 别名 中为一个专门的类模板定义一个类型。使用相同(但未知)的类模板类型并修改包含的类型。
如何推断别名的类模板类型?
我尝试使用模板模板参数$ clang++ prog.cc -Wall -Wextra -std=c++14 -pedantic:
// ----------------------
// third-party header file; may not be modified
template<typename V>
struct UnknownContainer
{
V m;
};
typedef UnknownContainer<int> KnownAlias;
// ----------------------
// my file (includes third-party header)
// only knows KnownAlias, not UnknownContainer
#include <iostream>
#include <cstdlib>
#include <type_traits>
template< template <typename> class C >
using MakeConstValueType = C< const int >;
typedef MakeConstValueType<KnownAlias> MyContainer;
// example usage
void foo(const MyContainer& c)
{
std::cout << "value = " << c.m << std::endl;
}
int main()
{
MyContainer c { 42 };
foo(c);
}
但我收到此错误:
prog.cc:23:28: error: template argument for template template parameter must be a class template or type alias template
typedef MakeConstValueType<KnownAlias> MyContainer;
^
有什么想法吗?
【问题讨论】: