【问题标题】:Can't use base class template member function不能使用基类模板成员函数
【发布时间】:2023-04-02 16:09:01
【问题描述】:

出于某种原因,我可以看到最上面的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&lt;dummy, int, float&gt;::fn(int&amp;&amp;) 失败,而 clang 和 VC++ 调用 X&lt;dummy, int, float&gt;::fn(float&amp;&amp;) 失败。

据我了解,编译器在调用 X&lt;dummy, int, float&gt;::fn(float&amp;&amp;) 时应忽略绝对基类成员函数 template&lt;typename R&gt; R X&lt;dummy&gt;::fn(R&amp;&amp;),因为该模板应解析为 float X&lt;dummy&gt;::fn(float&amp;&amp;),这与派生成员函数 float X&lt;dummy, float&gt;::fn(float&amp;&amp;) 完全匹配,需要派生出一个可以毫不含糊地调用。

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

编辑

套用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++ 下工作,但在clangVC++ 下有同样的问题。那么,这里只有 g++ 符合,其余的都是有缺陷的吗?

【问题讨论】:

  • 由于 enable_if_t 仅在两件事上进行了模板化,我不确定您为什么以可变方式定义宏。
  • @NirFriedman,原因是可能有一个逗号会让宏认为有多个参数。
  • @T.C. #2 我需要做什么?我希望隐藏基类成员函数。
  • @T.C.,但确实如此。请参阅更新后的问题和my comment您的问题。
  • @TC,好吧,如果我不正确的是,当基类和派生类具有导致相同 sig 的成员函数(不包括模板参数和返回类型)时,只有派生类才会使用using base::member_function 语法时可见,请赐教。这看起来很简单。

标签: c++ c++14


【解决方案1】:

两件事:

  1. fn 的基准包罗万象版本与其他 fn 变体一样好;因此,充其量你会得到一个模棱两可的过载错误。

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

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

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


C++ 中重载解析的工作方式是:

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

隐藏在第一步操作:如果一个声明被隐藏,它不会通过名称查找找到,因此它不在初始候选集中,并且在任何情况下都不会被重载解析考虑 /em>,无论步骤 2、3 或 4 中发生什么。隐藏声明实际上不存在用于重载解析。

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

【讨论】:

  • 为什么不考虑生成的实例化函数签名(不包括模板参数列表)?这听起来既不直观也不实用。
  • 我希望隐藏具有相同sig(不包括模板sig)的基本功能。在第一个例子中,X&lt;dummy, int, float&gt; 的成员函数fn 签名是float(float&amp;&amp;)。它的基类 sig 将是 int(int&amp;&amp;),而最终基类的 sig 将是任何与 template&lt;typename T&gt; T(T&amp;&amp;) 匹配的东西。当尝试调用X&lt;dummy, int, float&gt;::fn(1.f) 时,应该有两个匹配项,float X&lt;dummy, float&gt;::fn(float&amp;&amp;)float X&lt;dummy&gt;::fn(float&amp;&amp;)(当模板被实例化时)。我是说,因为它们是一样的,所以基地应该被隐藏并且......
  • ... 应该只匹配float X&lt;dummy, float&gt;::fn(float&amp;&amp;)。这就是我想要的,根据你的第 2 点,这就是应该发生的事情。我的理解是,您的第 1 点不正确,因为您的第 2 点。
  • 有趣...您发布的link 似乎另有说明。 member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list, cv-qualification。参数类型列表指向一个链接,该链接声明:...After producing the list of parameter types...forming the function type. The resulting list...is the function's parameter-type-list,推断...
  • @Adrian 你意识到函数模板也有参数类型列表,甚至在替换之前?
猜你喜欢
  • 2010-12-13
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 2011-02-12
相关资源
最近更新 更多