Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C2995:已定义模板_C++_Visual Studio_Sfinae_Friend - Fatal编程技术网

C++ C2995:已定义模板

C++ C2995:已定义模板,c++,visual-studio,sfinae,friend,C++,Visual Studio,Sfinae,Friend,我已经阅读了它的答案,但仍然不明白为什么我会遇到这个问题 此代码在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&am

我已经阅读了它的答案,但仍然不明白为什么我会遇到这个问题

此代码在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);
}
#包括
#包括
模板
结构A
{
模板
friend std::enable_if_t::value,A>运算符&&(a1,a2)
#如果1
;
#否则
{
返回{};
}
#恩迪夫
};
#如果1
模板
std::enable_if_t::value,A>运算符&&(a1,a2)
{
返回{};
}
#恩迪夫
int main()
{
std::字符串s;
A a1;
A a2,a3;
a3=a1和a2;

标准中实际上有一条规则说这是一个错误,来自:

然而,为了根据和确定实例化的重新声明是否有效,与模板中的定义相对应的声明被视为定义

标准中的示例:

模板结构友好{
模板友元int f(U){return sizeof(T);}
};
友好的fc;
Friendly ff;//错误:生成f(U)的第二个定义
在您的示例中,
A
A
的实例化中实例化友元函数时,
A将被实例化。这将生成友元函数的第二个定义。

也许全局中已经有一个“1”模板?尝试将“1”更改为唯一的,看看是否会得到相同的结果。