我得到了一个模板方法类,看看这个:
struct undefined {};
template<typename T> struct is_undefined : mpl::false_ {};
template<> struct is_undefined<undefined> : mpl::true_ {};
template<class C>
struct foo {
template<class F, class V>
typename boost::disable_if<is_undefined<C> >::type
apply(const F &f, const V &variables) {
}
template<class F, class V>
typename boost::enable_if<is_undefined<C> >::type
apply(const F &f, const V &variables) {
}
};
Run Code Online (Sandbox Code Playgroud)
显然,两个模板都被实例化,导致编译时错误.是否实例化模板方法不同于自由函数的实例化?我已经解决了这个问题,但我想知道是什么.我唯一能想到的可能会导致这种行为,启用条件不依赖于立即模板参数,而是依赖于类模板参数
谢谢
如果用户尝试使用给定的模板参数调用所述函数,那么如何在编译时专门化模板函数来生成错误?
通过使用以下习语,我能够为模板类获得此行为...
template <typename T>
class MyClass< std::vector<T> >;
Run Code Online (Sandbox Code Playgroud)
我试图修改的函数的基本签名是......
template <typename T>
T bar(const int arg) const {
...
}
Run Code Online (Sandbox Code Playgroud)
如果我使用与以前禁用某些模板类相同的范例......
template<>
std::string foo::bar(const int arg) const;
Run Code Online (Sandbox Code Playgroud)
我可以生成链接器错误,我认为这比运行时错误更令人满意,但仍然不是我正在寻找的.
因为我不能够使用C++ 11,我不能使用static_assert,如所描述这里.相反,我试图这样使用BOOST_STATIC_ASSERT......
template<>
std::string foo::bar(const int arg) const {
BOOST_STATIC_ASSERT(false);
return "";
}
Run Code Online (Sandbox Code Playgroud)
但是,这会产生以下编译时错误,即使我没有尝试使用我试图禁止的模板参数调用该函数的实例...
error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>'
Run Code Online (Sandbox Code Playgroud)
我找到了这篇文章,但它并没有真正提供我觉得适用于我的任何见解.有人可以帮忙吗?