【发布时间】:2018-02-14 14:03:14
【问题描述】:
我编写了一个 c++ 类,通过在测试程序中使用它可以正常工作。通过在我的项目中使用它,出现编译错误 C2976。所以我减少了代码来显示问题:
source.h
#include <type_traits>
//#include <boost/asio.hpp>
namespace myNS {
class Dummy {
public:
Dummy() {}
~Dummy() {}
template<class T> struct _isValidType : std::false_type {};
template<> struct _isValidType<bool> : std::true_type {};
template<> struct _isValidType<int> : std::true_type {};
template<class T> struct isValidType : _isValidType<std::remove_cv_t<T>>::type {};
template<typename OUT, typename std::enable_if<isValidType<OUT>::value>::type* dummy = nullptr>
OUT doSomething() { OUT out{}; return out; }
};
} /* namespace myNS */
source.cpp
#include "source.h"
int main() {
myNS::Dummy d;
bool b = d.doSomething<bool>();
}
不包含 boost::asio 时没有编译错误。通过包含 boost::asio 编译器在最后一个模板定义上运行错误:
source.h(14): error C2976: "Dummy::isValidType": Nicht genügend Vorlage-Argumente.
source.h(12): note: Siehe Deklaration von "Dummy::isValidType"
source.h(14): error C2955: "Dummy::isValidType" : Für die Verwendung von Klasse Vorlage ist eine Vorlage-Argumentliste erforderlich
source.h(12): note: Siehe Deklaration von "Dummy::isValidType"
source.h(15): error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.
有人知道这个问题吗?是 MSVC 的 boost::asio 的错,还是我的错?在这个例子中 boost::asio 不是必须包含的,但在我的项目中我必须包含它。
我正在使用 MS Visual Studio Community 2017 V 15.5.5 并使用 boost 库 v1.66.0。
谢谢!
【问题讨论】:
-
你怎么打电话给
doSomething()? -
您不能在类范围内显式特化模板。将这些显式特化移到命名空间范围内。
-
我在使用命名空间时遇到了同样的问题
-
@werner 编译良好:wandbox.org/permlink/kq40FRS2rOmo98Sv
标签: c++ templates boost enable-if asio