gpt4 book ai didi

c++ - 特殊类模板的类外构造函数定义

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:05:32 25 4
gpt4 key购买 nike

我正在尝试为类定义之外的显式专用类模板定义一个构造函数,如下所示:

template <typename T>
struct x;

template <>
struct x<int> {
inline x();

/* This would have compiled:
x() {
}
*/
};

template <> // Error
x<int>::x() {
}

不过好像是报错了。 Comeau 说:error: "x<int>::x()" is not an entity that can be explicitly specialized ,即使完整的类是特化的。

这里有什么问题?

最佳答案

不要指定 template<>对于定义:

template <typename T>
struct x;

template <>
struct x<int> {
x();
};

inline x<int>::x(){}

编辑:构造函数定义不是特化,所以 template<>是不必要的。它是特化构造函数的定义。因此,您只需像任何其他非模板类一样指定类型。

关于c++ - 特殊类模板的类外构造函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3935916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com