【发布时间】:2021-02-22 16:24:27
【问题描述】:
我已阅读 this question 及其答案,但仍然不明白为什么会遇到此问题。
这段代码在VS2017中编译:
#include <iostream>
#include <string>
template <class T>
struct A
{
template<class U>
friend std::enable_if_t<!std::is_void<U>::value, A<void>> operator&&(A<U> a1, A<void> a2)
#if 1
;
#else
{
return {};
}
#endif
};
#if 1
template<class U>
std::enable_if_t<!std::is_void<U>::value, A<void>> operator&&(A<U> a1, A<void> a2)
{
return {};
}
#endif
int main()
{
std::string s;
A<int> a1;
A<void> a2, a3;
a3 = a1 && a2;
std::cout << "Press ENTER to exit.\n";
std::getline(std::cin, s);
}
但是如果我将#if 1s 更改为#if 0s,我会得到 C2995: template function has been defined。
为什么我在struct A里面定义了friend函数编译失败,但是在外面定义却编译成功了?
【问题讨论】:
-
也许全局中已经有一个“1”模板?尝试将“1”更改为独特的值,看看是否会产生相同的结果。
标签: c++ visual-studio sfinae friend