如何在不声明类型的情况下将模板发送到另一个模板
How to send a template to another template without declaring the type?
我正在尝试编译以下代码:
#include <iostream>
template<class T> struct Container1;//forward declaration
template<class T> struct Container2;//forward declaration
template<class Container,class Type>
using _Make_Container=typename Container<Type>;
template<class T>
struct Container1
{};
template<class T>
struct Container2
{};
int main()
{
_Make_Container<Container1,int> a;
}
给了我一些错误:
expected nested-name-specifier before 'Container'
using _Make_Container=typename Container<Type>;
^
error: 'Container' is not a template
对我来说似乎没问题,因为我们将Container1
和int
发送到using
,然后它变成:
using _Make_Container=typename Container1<int>;
//then
Container1<int> a;
我不明白那个错误的原因!!任何想法?
这些错误信息非常清楚。
using _Make_Container=typename Container<Type>; ^
您没有引用嵌套依赖类型,因此这里不需要typename
关键字。阅读这篇文章,了解typename
在哪些地方是必需的。
错误:'Container'不是模板
就像它说的,你的别名模板不表明Container
是一个模板,但它继续使用它作为一个模板。您需要为Container
template<template<typename> class Container,class Type>
using _Make_Container = Container<Type>;
借鉴Yakk的优秀建议,您应该将别名模板定义更改为
template<template <typename...> class T1, typename... T2>
using Make_Container = T1<T2...>;
这允许您执行以下操作:
template<class T, class T1>
struct Container2
{};
Make_Container<Container2,int,long> b;
最后,使用前导下划线后跟大写字母的名称由实现保留。所以你应该把_Make_Container
重命名为Make_Container
。
您必须像这样编写别名模板
template<template <typename> class T1,class T2>
using _Make_Container= T1<T2>;
template< template <class> class Container,class Type>
using _Make_Container = Container<Type>;
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 如何在不知道对应关系的情况下在字符串中搜索字符并将其分配给另一个字符?
- 在没有任何锁的情况下加入另一个线程后是否需要内存围栏?
- 是否可以在不扩展初始宏的情况下将一个宏作为参数提供给另一个宏?
- 如何在不定义目标变量大小的情况下逐个字符将字符串变量复制到另一个字符
- 如何在不复制的情况下将过滤向量的结果存储在另一个向量中
- 如何在没有窗口类的情况下编写QT系统托盘应用程序,并将其与另一个进程集成
- 如何在不复制的情况下将C++类从一个项目重用到另一个项目
- 模板化类,如何在不使此类模板化的情况下表示为另一个类的成员变量
- 如何在不使用友元类的情况下通过另一个类访问一个类的私有成员
- 在另一个线程正在运行的情况下退出应用程序时出错
- 如何在不使用静态变量的情况下从递归函数中只调用另一个函数一次
- Qt/QML:如何在不重新启动应用程序的情况下"switch to"或"open"另一个 QML 文件?
- (威纳皮C++)如何在没有全局变量的情况下将数据从一个窗口传递到另一个窗口
- 如何在 QRC 文件中的另一个资源下添加一个 qresource
- 如何在不冻结应用程序 1 的情况下从另一个应用程序 1 执行应用程序 2
- 如何在不使用头文件的情况下以'function'形式访问另一个C++文件?