21

This code is from "C++ programming language" by Bjarne Stroustrup (C.13.8.3 Point of Instantiation Binding)

template <class T>
void f(T value)
{
    g(value);
}

void g(int v);

void h()
{
    extern g(double);
    f(2);
}

And he mentions:

Here, the point of instantiation for f() is just before h(), so the g() called in f() is the global g(int) rather than the local g(double). The definition of ‘‘instantiation point’’ implies that a template parameter can never be bound to a local name or a class member.

void h()
{
    struct X {}; // local structure
    std::vector<X> v; // error: can't use local structure as template parameter
}

My questions are:

  1. Why should the first code work? g() is declared later, and I really get an error with G++ 4.9.2 that g isn't declared at that point.

  2. extern g(double) - how this works? since return value doesn't matter in case of function overloading, then we can miss it in forward declarations?

  3. the point of instantiation for f() is just before h() - why? isn't it logical that it'll get instantiated when f(2) is being called? Right where we call it, whence g(double) will be in scope already.

  4. The definition of ‘‘instantiation point’’ implies that a template parameter can never be bound to a local name or a class member - Has this changed in C++14? I'm getting error with C++(G++ 4.9.2), but don't get error with C++14(G++ 4.9.2).

4

1 回答 1

15

“1985 年,第一版 C++ 编程语言发布,成为该语言的权威参考,因为当时还没有官方标准。” wiki C++ History所以它在 C++11 和 C++14 之间没有改变。我可以假设(请谨慎对待)它在“预标准化”和标准化之间发生了变化。也许更了解 C++ 历史的人可以在这里阐明更多信息。

至于实际发生的事情:


首先让我们摆脱简单的方式:

extern g(double);

这是无效的 C++。从历史上看,不幸的是 C 允许省略类型。在 C++ 中,您必须编写extern void g(double).


接下来,让我们忽略g(double)重载来回答您的第一个问题:

template <class T>
void f(T value)
{
    g(value);
}

void g(int v);

int main()
{
    f(2);
}

在 C++ 中有臭名昭著的两阶段名称查找:

  • 在第一阶段,在模板定义中,所有非依赖名称都被解析。不这样做是一个硬错误;
  • 从属名称在模板实例化的第二阶段解析。

规则有点复杂,但这就是它的要点。

g取决于模板参数T,因此它通过了第一阶段。这意味着如果你从不实例化f,代码编译得很好。在第二阶段fT = int. g(int)现在已搜索,但未找到:

17 : error: call to function 'g' that is neither visible in the template definition nor found by argument-dependent lookup
g(value);
^
24 : note: in instantiation of function template specialization 'f<int>' requested here
f(2);
^
20 : note: 'g' should be declared prior to the call site
void g(int v);

为了让任意名称g以优异的成绩通过,我们有几个选项:

  1. 之前声明g
void g(int);

template <class T>
void f(T value)
{
    g(value);
}
  1. 带入:g_T
template <class T>
void f(T)
{
    T::g();
}

struct X {
   static void g();
};

int main()
{
    X x;
    f(x);
}
  1. 通过ADLg引入T
template <class T>
void f(T value)
{
    g(value);
}

struct X {};

void g(X);

int main()
{
    X x;
    f(x);
}

这些当然会改变程序的语义。它们旨在说明您在模板中可以拥有和不能拥有的内容。


至于为什么 ADL 没有找到g(int),但是发现g(X)

§ 3.4.2 依赖于参数的名称查找 [basic.lookup.argdep]

  1. 对于函数调用中的每个参数类型 T,有一组零个或多个关联的命名空间和一组零个或多个关联的类被视为 [...]:

    • 如果 T 是基本类型,则其关联的命名空间和类集都是空的。

    • 如果 T 是类类型(包括联合),则其关联的类是:类本身;它所属的类别(如有的话);及其直接和间接基类。其关联名称空间是其关联类是其成员的名称空间。[...]


最后我们得到了为什么没有找到mainextern void g(double);内部:首先我们证明了它g(fundamental_type)是在定义之前声明的。所以让我们把它放在里面。ADL 找到了吗?fvoid g(X)main

template <class T>
void f(T value)
{
    g(value);
}

struct X{};


int main()
{
  X x;
  void g(X);

  f(x);
}

不,因为它与X(即全局命名空间)不在同一个命名空间中,所以 ADL 找不到它。

g不在全局范围内的证明

int main()
{
  void g(X);

  X x;
  g(x); // OK
  ::g(x); // ERROR
}

34:错误:全局命名空间中没有名为“g”的成员;你是说简单的“g”吗?

于 2016-10-10T13:45:39.767 回答