在类template的成员模板函数上启用le_if
enable_if on member template function of class template
这似乎是MSVC10中的一个bug ?
#include <type_traits>
template<int j>
struct A{
template<int i>
typename std::enable_if<i==j>::type
t(){}
};
int main(){
A<1>().t<1>(); //error C2770
}
错误C2770:无效的显式template_or_generic参数"enable_if:: A型::t(空白)"。
以下编译:
#include <type_traits>
template<class j>
struct A{
template<class i>
typename std::enable_if<std::is_same<i,j>::value>::type
t(){}
};
template<unsigned int j>
struct B{
template<unsigned int i>
typename std::enable_if<i==j>::type
t(){}
};
int main(){
A<int>().t<int>();
B<1>().t<1>();
}
这似乎是MSVC2010部分的一些奇怪行为,它无法确定您使用<1>作为模板参数是否是基于int的模板的实例化。
当我编译上面的代码时,我得到以下详细错误:
error C2770: invalid explicit template argument(s) for
'std::enable_if<i==1>::type A<j>::t(void)'
with
[
j=1
]
d:programmingstackoverflowstackoverflowstackoverflow.cpp(11) :
see declaration of 'A<j>::t'
with
[
j=1
]
如果你把1换成0,你会发现它仍然不能工作,但如果你使用任何其他有效的int,模板似乎可以很愉快地编译。
我不完全确定为什么会发生这种情况,但是您可以通过使用const int来表示模板形参来使这段代码工作:
template<int j>
struct A{
template<int i>
typename std::enable_if<i == j>::type
t(){}
};
int main(){
const int j = 1;
const int i = 1;
A<j>().t<i>(); //now compiles fine
}
基于此,我怀疑编译器在模板实例化时发现0和1的使用有歧义。
相关文章:
- 编译时未启用intel oneApi CUDA支持
- OpenGL在启用深度测试时不会丢弃我的碎片
- Visual C++GC接口如何启用它以及要包含哪个库
- 如何在自定义类中启用'auto loops'?
- 根据某个函数是否存在启用模板
- 视觉工作室 2017;启用 /permissive 时,类型 "const wchar_t *" 的参数与类型 "PWSTR" 的参数不兼容
- 启用从"vector<const T>&"到"const vector&"的隐式转换<T>
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 在 Eclipse 上启用 C++17 以使用 'std::byte'
- 当 noexcept 函数尝试在 gcc 或 clang 中调用非 noexcept 函数时启用警告
- 如何为一段代码启用 -permissive
- 是否可以/希望创建不可复制的共享指针模拟(以启用weak_ptr跟踪/借用类型语义)?
- 在 GCC 中启用"differing levels of indirection"警告/错误
- 使用 Doxygen 记录枚举类值,而不启用EXTRACT_ALL
- '_HAS_CXX17'宏是否可用于自定义项目标头以启用C++17 语言集功能?
- 如何启用转换模板参数 T 以常量 T?
- 在使用 gSoap 链接多个 SOAP 服务时启用保持活动状态
- 如何启用标准::实验::atomic_shared_pointer
- 如何启用友元类的友元功能直接在C++中访问其私有成员