gpt4 book ai didi

c++ - 运算符重载导致模板参数推导失败

转载 作者:行者123 更新时间:2023-11-30 04:51:20 24 4
gpt4 key购买 nike

我有以下示例代码,它在 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;
}

编译输出

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’

如果你能给我一个修复它的建议,我将不胜感激。

最佳答案

Template argument deduction由于类型不匹配而失败; std::array 的第二个非类型模板参数属于 std::size_t 类型。

(强调我的)

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, ...

将非类型参数从 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;
}

关于c++ - 运算符重载导致模板参数推导失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54839988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com