当重载具有多重继承的函数时,GCC称调用它是不明确的,但Clang和MSVC不这样做
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当重载具有多重继承的函数时,GCC称调用它是不明确的,但Clang和MSVC不这样做相关的知识,希望对你有一定的参考价值。
我正在使用这个变体库:https://github.com/cbeck88/strict-variant。它提供了类似于std::variant
和boost::variant
的类。鉴于此struct
:
struct S
{
explicit S(double) {}
};
我想做这个:
strict_variant::variant<double, S> v = 2.0;
这适用于Clang 5.0.1和MSVC 19.12.25831.00,但无法使用GCC 7.2.1进行编译。
我查看了库的代码并将问题减少到了:
#include <iostream>
struct S
{
constexpr S() {}
constexpr explicit S(double) {}
};
template<unsigned i> struct init_helper;
template<> struct init_helper<0> { using type = double; };
template<> struct init_helper<1> { using type = S; };
template<unsigned i>
struct initializer_leaf
{
using target_type = typename init_helper<i>::type;
constexpr unsigned operator()(target_type) const
{
return i;
}
};
struct initializer : initializer_leaf<0>, initializer_leaf<1>
{
};
int main()
{
std::cout << initializer()(double{}) << " = double" << '
';
std::cout << initializer()(S{}) << " = S" << '
';
return 0;
}
输出是
0 = double
1 = S
GCC说:
strict_variant_test.cpp: In function ‘int main()’:
strict_variant_test.cpp:29:37: error: request for member ‘operator()’ is ambiguous
std::cout << initializer()(double{}) << " = double" << '
';
^
strict_variant_test.cpp:17:21: note: candidates are: constexpr unsigned int initializer_leaf<i>::operator()(initializer_leaf<i>::target_type) const [with unsigned int i = 1; initializer_leaf<i>::target_type = S]
constexpr unsigned operator()(target_type) const
^~~~~~~~
strict_variant_test.cpp:17:21: note: constexpr unsigned int initializer_leaf<i>::operator()(initializer_leaf<i>::target_type) const [with unsigned int i = 0; initializer_leaf<i>::target_type = double]
strict_variant_test.cpp:30:32: error: request for member ‘operator()’ is ambiguous
std::cout << initializer()(S{}) << " = S" << '
';
^
strict_variant_test.cpp:17:21: note: candidates are: constexpr unsigned int initializer_leaf<i>::operator()(initializer_leaf<i>::target_type) const [with unsigned int i = 1; initializer_leaf<i>::target_type = S]
constexpr unsigned operator()(target_type) const
^~~~~~~~
strict_variant_test.cpp:17:21: note: constexpr unsigned int initializer_leaf<i>::operator()(initializer_leaf<i>::target_type) const [with unsigned int i = 0; initializer_leaf<i>::target_type = double]
但是,当我将initializer
的定义更改为此时,它适用于GCC(仍然是Clang和MSVC):
struct initializer
{
constexpr unsigned operator()(double) const
{
return 0;
}
constexpr unsigned operator()(S) const
{
return 1;
}
};
我对C ++的理解说这是等价的,所以我认为这是GCC中的一个错误,但我经常遇到标准说出令人惊讶的事情并且我的假设是错误的问题。所以,我的问题是:谁的错是这个? GCC是否有错误,Clang和MSVC是否有错误,或者是未定义/未指定的代码解释,以便所有编译器都正确?如果代码错误,怎么修复?
这实际上是一个铿锵的bug。
经验法则是不同范围内的名称不会超载。这是一个简化的例子:
template <typename T>
class Base {
public:
void foo(T ) { }
};
template <typename... Ts>
struct Derived: Base<Ts>...
{};
int main()
{
Derived<int, double>().foo(0); // error
}
这应该是一个错误,因为class member lookup rules状态基本上只有一个基类可以包含给定的名称。如果多个基类具有相同的名称,则查找是不明确的。这里的解决方案是使用using-declaration将两个基类名称带入派生类。在C ++ 17中,使用声明可以是包扩展,这使得这个问题变得更加容易:
template <typename T>
class Base {
public:
void foo(T ) { }
};
template <typename... Ts>
struct Derived: Base<Ts>...
{
using Base<Ts>::foo...;
};
int main()
{
Derived<int, double>().foo(0); // ok! calls Base<int>::foo
}
对于特定的库,this code:
template <typename T, unsigned... us> struct initializer_base<T, mpl::ulist<us...>> : initializer_leaf<T, us>... { static_assert(sizeof...(us) > 0, "All value types were inelligible!"); };
应该是这样的:
template <typename T, unsigned... us>
struct initializer_base<T, mpl::ulist<us...>> : initializer_leaf<T, us>... {
static_assert(sizeof...(us) > 0, "All value types were inelligible!");
using initializer_leaf<T, us>::operator()...; // (*) <==
};
(虽然我猜这个库的目标是C ++ 11,所以我提交了一个符合C ++ 11标准的修复程序......这只是更冗长一点)。
以上是关于当重载具有多重继承的函数时,GCC称调用它是不明确的,但Clang和MSVC不这样做的主要内容,如果未能解决你的问题,请参考以下文章