简体   繁体   English

模板参数推导失败,运算符重载

[英]Template argument deduction failure with operator overloading

I have the following sample code which compile fine in Visual Studio but not in GCC 6. I understanding it may related to template argument deduction failure, but I can't think of a correct solution to solve the problem.我有以下示例代码,它们在 Visual Studio 中编译得很好,但在 GCC 6 中却没有。我理解它可能与模板参数推导失败有关,但我想不出解决问题的正确解决方案。

#include <iostream>
#include <array>

//using namespace std;
namespace 
{
    template <int T>
    std::array<char, T> operator&(const std::array<char, T>& l, const std::array<char, T>& r)
    {
        std::array<char, T> result{};

        return result;
    }

}
int main()
{
    std::array<char, 4> result{};
    std::array<char, 4> value{};
    std::array<char, 4> mask{};
    result = value & mask;
    return 0;
}

Compile output编译输出

main.cpp: In function ‘int main()’:
main.cpp:29:20: error: no match for ‘operator&’ (operand types are ‘std::array’ and ‘std::array’)
 result = value & mask;
                ^
main.cpp:16:25: note: candidate: template std::array {anonymous}::operator&(const std::array&, const std::array&)
 std::array<char, T> operator&(const std::array<char, T>& l, const std::array<char, T>& r)
                     ^
main.cpp:16:25: note:   template argument deduction/substitution failed:
main.cpp:29:22: note:   mismatched types ‘int’ and ‘long unsigned int’
 result = value & mask;
                  ^
main.cpp:29:22: note:   ‘std::array’ is not derived from ‘const std::array’
In file included from /usr/include/c++/5/ios:42:0,
             from /usr/include/c++/5/ostream:38,
             from /usr/include/c++/5/iostream:39,
             from main.cpp:9:
/usr/include/c++/5/bits/ios_base.h:165:3: note: candidate: constexpr 
std::_Ios_Iostate std::operator&(std::_Ios_Iostate, std::_Ios_Iostate)
operator&(_Ios_Iostate __a, _Ios_Iostate __b)
^
/usr/include/c++/5/bits/ios_base.h:165:3: note:   no known conversion for 
argument 1 from ‘std::array’ to ‘std::_Ios_Iostate’
/usr/include/c++/5/bits/ios_base.h:125:3: note: candidate: constexpr 
std::_Ios_Openmode std::operator&(std::_Ios_Openmode, std::_Ios_Openmode)
operator&(_Ios_Openmode __a, _Ios_Openmode __b)
^
/usr/include/c++/5/bits/ios_base.h:125:3: note:   no known conversion for 
argument 1 from ‘std::array’ to ‘std::_Ios_Openmode’
/usr/include/c++/5/bits/ios_base.h:83:3: note: candidate: constexpr 
std::_Ios_Fmtflags std::operator&(std::_Ios_Fmtflags, std::_Ios_Fmtflags)
operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
^
/usr/include/c++/5/bits/ios_base.h:83:3: note:   no known conversion for 
argument 1 from ‘std::array’ to ‘std::_Ios_Fmtflags’

Greatly appreciated if you can give me a suggestion to fix it.如果您能给我一个修复它的建议,将不胜感激。

Template argument deduction fails because of type mismatch;由于类型不匹配, 模板参数推导失败; the 2nd non-type template parameter of std::array is of type std::size_t . std::array的第二个非类型模板参数的类型为std::size_t

(emphasis mine) (强调我的)

If a non-type template parameter is used in the parameter list, and the corresponding template argument is deduced, the type of the deduced template argument ( as specified in its enclosing template parameter list, meaning references are preserved) must match the type of the non-type template parameter exactly , ...如果在形参列表中使用了非类型模板形参,并推导出了相应的模板形参,则所推导出的模板形参的类型(在其封闭的模板形参列表中指定,意味着保留引用)必须匹配非类型模板参数恰好, ...

Change the non-type parameter from int to std::size_t .将非类型参数从int更改为std::size_t

template <std::size_t T>
std::array<char, T> operator&(const std::array<char, T>& l, const std::array<char, T>& r)
{
    std::array<char, T> result{};

    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM