I have read this question and its answer and still don't understand why I am encountering this issue.
This code compiles in 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);
}
But if I change the #if 1
s to #if 0
s, I get C2995: template function has already been defined.
Why does the compilation fail if I define the friend function inside struct A
but succeed if I define it outside?