在构造函数声明中使用 class 本地类型定义
using class local type definition in constructor declaration
我试图在构造函数声明中使用 class 本地类型定义。 classes 都是模板,这里是代码。
template < typename T>
class complexType
{
public:
using value_type = T;
complexType( T t ) {}
};
template <typename containedType >
class container
{
public:
container ( containedType::value_type v ) { return; }
//container ( int v ) { return; }
};
int main(int ac, char **av)
{
container <complexType<int>> c(100);
return 0;
}
如果我使用传递 int 的第二个构造函数定义,代码构建良好。我无法解释为什么代码无法构建。
value_type
是依赖名,依赖于模板参数,在这种情况下你需要使用typename
来表示value_type
是type:
template <typename containedType >
class container
{
public:
container ( typename containedType::value_type v ) { return; }
^^^^^^^
//container ( int v ) { return; }
};
我试图在构造函数声明中使用 class 本地类型定义。 classes 都是模板,这里是代码。
template < typename T>
class complexType
{
public:
using value_type = T;
complexType( T t ) {}
};
template <typename containedType >
class container
{
public:
container ( containedType::value_type v ) { return; }
//container ( int v ) { return; }
};
int main(int ac, char **av)
{
container <complexType<int>> c(100);
return 0;
}
如果我使用传递 int 的第二个构造函数定义,代码构建良好。我无法解释为什么代码无法构建。
value_type
是依赖名,依赖于模板参数,在这种情况下你需要使用typename
来表示value_type
是type:
template <typename containedType >
class container
{
public:
container ( typename containedType::value_type v ) { return; }
^^^^^^^
//container ( int v ) { return; }
};