【发布时间】:2017-07-21 10:03:38
【问题描述】:
我正在学习 C++11x 中的 lambda 表达式,我用谷歌搜索了相同的内容,并在下面的代码 sn-p 中发现模板类型名用作继承。
说,
template< class T >
struct MyType : T {
....
当我编译代码时,它没有给出任何错误。但是当我尝试为 struct MyType 创建一个实例时,它导致了错误。
// Example program
#include <iostream>
#include <string>
template< class T >
struct MyType : T {
static const auto data = 0;
static const size_t erm = sizeof(data);
};
int main()
{
struct MyType<int> my;
std::cout<<"\n test ";
return 0;
}
编译上述代码时出错:
In instantiation of 'struct MyType<int>':
15:22: required from here
6:8: error: base type 'int' fails to be a struct or class type In function 'int main()':
15:22: warning: unused variable 'my' [-Wunused-variable]
请添加一些光。为什么编译在实例化结构时会出错?另一方面,为什么声明没有给出任何错误?
提前致谢。
【问题讨论】:
-
type 参数用作您的类型的基类。
int不是一个类,你不能从它继承。您需要另一个结构(或类)作为您的基础。我认为错误消息 base typeintfailed to be a struct or class 很清楚...
标签: c++ templates inheritance