Reputation: 13
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.
#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.
Upvotes: 1
Views: 251
Reputation: 172894
Template argument deduction fails because of type mismatch; the 2nd non-type template parameter of std::array
is of type 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
.
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;
}
Upvotes: 1