c++ - 不能使用基类模板成员函数

标签 c++ c++14

由于某种原因,我可以看到最上面的template<typename T> X<...>::fn(T&&) ,但不是基类版本。使用 using 导入它们关键字不起作用。据我了解:

In Class definition

... If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.

示例代码:

#include <iostream>
#include <type_traits>

#define ENABLE_IF(...) std::enable_if_t<__VA_ARGS__, int> = 0

struct dummy {};

template<typename T>
struct always_false : std::false_type {};

template<typename...Ts>
struct X;

template<typename Tx, typename T, typename...Ts>
struct X<Tx, T, Ts...> : X<Tx, Ts...>
{
    using X<Tx, Ts...>::fn;

    template<typename R, ENABLE_IF(std::is_same<T, R>::value)>
    auto fn(R&& x)
    {
        return x;
    }
};

template<typename Tx>
struct X<Tx>
{
    template<typename R>
    auto fn(R&& x)
    {
        static_assert(always_false<R>::value, "Invalid type");
    }
};

int main()
{
    X<dummy, int, float> x;
    std::cout << x.fn(1) << std::endl;
    std::cout << x.fn(1.f) << std::endl;
    std::cout << "Hello, world!\n";
}

我已经在 g++、clang 和 VC++ 上试过了,它们都有各种错误,(ambiguous callmember function disabledcould not deduce)。有趣的是,g++ 在调用 X<dummy, int, float>::fn(int&&) 时失败了。 ,而 clang 和 VC++ 无法调用 X<dummy, int, float>::fn(float&&) .

据我了解,编译器应该忽略绝对基类成员函数template<typename R> R X<dummy>::fn(R&&)打电话时 X<dummy, int, float>::fn(float&&) ,因为该模板应解析为 float X<dummy>::fn(float&&)与派生成员函数 float X<dummy, float>::fn(float&&) 完全匹配要求派生的调用没有歧义。

我做错了什么?我有什么不明白的?

编辑

释义T.C.'s answer so far ,“这就是规范所说的”,我认为这不是正确的解释。那里给出的两点相互矛盾。如果它们匹配得同样好(第 1 点),则只有最派生的函数签名应该可见(第 2 点)。

无论如何,如果问题是规范问题,那么如果我禁用匹配重载的可能性(这会导致歧义),它应该就会消失。因此,以下应该有效:

#include <iostream>
#include <type_traits>

#define ENABLE_IF(...) std::enable_if_t<__VA_ARGS__, int> = 0


template<typename T>
struct always_false : std::false_type {};


template<typename...Ts>
struct list {};


template<typename...Ts>
struct is_one_of;

template<template <typename...> class TT, typename T, typename T1, typename...Ts>
struct is_one_of<T, TT<T1, Ts...>> : is_one_of<T, TT<Ts...>> {};

template<template <typename...> class TT, typename T, typename...Ts>
struct is_one_of<T, TT<T, Ts...>> : std::true_type {};

template<template <typename...> class TT, typename T>
struct is_one_of<T, TT<>> : std::false_type {};


template<typename...Ts>
struct X;

template<typename L, typename T, typename...Ts>
struct X<L, T, Ts...> : X<L, Ts...>
{
    using X<L, Ts...>::fn;

    template<typename R, ENABLE_IF(std::is_same<T, R>::value)>
    constexpr auto fn(R&& x) const
    {
        return x;
    }
};

template<typename L>
struct X<L>
{
    template<typename R, ENABLE_IF(!is_one_of<R, L>::value)>
    constexpr auto fn(R&& x) const
    {
        static_assert(always_false<R>::value, "Type R didn't match");
    }
};


template<typename...Ts>
struct XX : X<list<Ts...>, Ts...> {};

int main()
{
    XX<int, float> x;
    std::cout << x.fn(1) << std::endl;
    std::cout << x.fn(2.f) << std::endl;
}

它确实在 g++ 下工作, 但在 clang 下有同样的问题和 VC++ .那么,这里是否只有 g++ 符合,其余的都是有缺陷的?

最佳答案

两件事:

  1. 您的 fn 基线包罗万象版本与其他 fn 变体同样匹配;因此充其量你会得到一个不明确的过载错误。

  2. using-declaration 的隐藏不考虑完整的签名(对于函数模板,这将包括模板参数列表)。它only considers 1) 名称,2)(函数)参数类型列表,3) cv 限定,以及 4) ref-qualifier(如果有的话)。如果所有四个都匹配,则基类函数模板将被隐藏并且不会由 using-declaration 引入。值得注意的是,不考虑模板参数列表。在您的情况下,各种 fn 之间唯一不同的是模板参数列表;它们都具有相同的名称、相同的参数类型列表、相同的 cv 限定符和相同的 ref 限定符(或没有)。因此,最派生的 fn 将从基类中隐藏所有 fn

    GCC 似乎没有将这部分实现到规范中,并在决定隐藏时考虑模板参数列表。

    这部分的一个可能修复方法是将 enable_if 移动到一个函数参数, 隐藏检查会考虑该参数。


C++ 中重载决策的工作方式是:

  1. 名称查找以构建一组候选函数和函数模板。
  2. 对于每个候选函数模板,执行模板参数推导。如果演绎失败,则将其从重载集中移除。如果推导成功,将候选函数模板替换为推导的特化。
  3. 从候选人集​​中消除不可行的候选人。
  4. 如果集合为空,则出错。否则,在候选函数中找到最佳可行函数。

隐藏在第一步操作:如果一个声明是隐藏的,它不会通过名称查找找到,因此它不在初始候选集中,并且不会被重载决议考虑在任何情况下,无论第 2、3 或 4 步发生什么。为了重载决议的目的,实际上不存在隐藏声明。

因此,在您的情况下,基类 fn 都是隐藏的。这是什么意思?这意味着通过名称查找找到的唯一候选者是来自最派生类的 int ,没有别的。如果模板参数推导和替换成功,将调用该函数模板。如果他们失败了(如 x.fn(2.f) 的情况),那么就没有可行的候选人了,你会得到一个错误。

关于c++ - 不能使用基类模板成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42917855/

相关文章:

c++ - C++11、14、17 或 20 是否为 pi 引入了标准常量?

c++ - 如何在编译时检查数组的大小

c++ - 调用表达式中 Callable 参数的完美转发目的?

c++ - 为什么这两个重载函数的声明方式不同?

c++ - Linux下使用c++如何知道一个文件是否正在被其他进程使用?

C++14遇到奇怪的 "use of deleted function"错误

c++ - void({}) 中的 {} 是什么?

c++ - 调用 xSemaphoreGive 后,xSemaphoreTake 不会恢复任务

c++ - 如何解析一个c_str

c++ - 具有虚拟关键字和字符的类的大小