I'm trying to define constexpr friend operator in template. I got a compiler error when trying to instantiate this operator in non-constexpr context. If I define the very same operator as a template class member of as a free template function, it works fine.
template <typename T>
struct A
{
T Value;
// error
friend constexpr bool operator== (const A& l, const A& r)
{
return l.Value == r.Value;
}
// ok
constexpr bool operator!= (const A& r) const
{
return Value != r.Value;
}
};
// ok
template <typename T> constexpr bool
operator< (const A<T>& l, const A<T>& r)
{
return l.Value < r.Value;
}
#include <string>
int main ()
{
A<std::string> s;
bool ret = (s < s, s == s, s != s);
}
The error I've got is
<source>(7): error C3615: constexpr function 'operator ==' cannot result in a constant expression
<source>(9): note: failure was caused by call of undefined function or one not declared 'constexpr'
<source>(9): note: see usage of 'std::operator =='
<source>(8): note: while compiling class template member function 'bool operator ==(const A<std::string> &,const A<std::string> &)'
<source>(29): note: see reference to class template instantiation 'A<std::string>' being compiled
Is this 'friend' discrimination is a requirement of the standard or a compiler bug?
Aucun commentaire:
Enregistrer un commentaire