代码之家  ›  专栏  ›  技术社区  ›  avakar

非限定查找和(可能)依赖的基类

c++
  •  5
  • avakar  · 技术社区  · 15 年前

    template <typename T>
    struct t
    {
        struct base { void f1(); };
        struct derived : base
        {
            void f2()
            {
                f1();
            }
        };
    };
    

    derived::f2 ,使用非限定查找 f1 . 威尔 base 被搜查?威尔 base::f1 被发现了?是 基础

    请引用标准中的话来证实你的答案。

    2 回复  |  直到 15 年前
        1
  •  8
  •   Johannes Schaub - litb    15 年前

    对, base 是这样的 f1 找不到。在C++ 03中,这是由 addition 14.6.1/2d (C++中没有98)和C++ 0x这是直接由 14.6.2.1/6

    它的依赖性很重要,因为以下是可能的

    // for T = int, f1 does not exist.
    template<> struct t<int>::base { };
    
        2
  •  0
  •   Matthieu M.    15 年前

    gcc拒绝代码:

    there are no arguments to 'f1' that depend on a template parameter, so a declaration of 'f1' must be available .

    然而引入 this-> 之前 f1 使其依赖,然后编译工作。

    • typedef (再说一遍)
    • 方法(您需要使用 using base::f1 this->f1

    • 合同一般条款3.4
    • VC++9语言
    • VC++10语言

    现在我们可以等待标准主义者从标准中得到准确的引用。