Igor R.
Igor R.

Reputation: 15075

Function candidates and declaration order

Consider the following snippet:

template<typename T>
int foo(T) {
  return 1;
}

struct my_struct{};

template<typename T>
int do_foo(T t) {
  return 
      foo(my_struct{}) + // 1   
      foo(t);            // 2 (via ADL)
}

int foo(my_struct) {
  return 2;
}

int main () {
  return do_foo(my_struct{});
} 

At first glance, the "non-dependent" call to foo will return 1, and the "dependent" one will return 2.

However, in the Standard we find the following paragraph:

If the call would <...> find a better match had the lookup within the associated namespaces considered all the function declarations with external linkage introduced in those namespaces in all translation units, not just considering those declarations found in the template definition and template instantiation contexts, then the program has undefined behavior.

It is unclear whether this paragraph speaks about dependent names lookup only - so I am wondering if it applies to the above code, thus making it ill-formed.

Could you please construct an example that would illustrate this paragraph's meaning?

Upvotes: 3

Views: 118

Answers (1)

Artyer
Artyer

Reputation: 40791

This entire paragraph only applies to functions resolved with ADL:

For a function call where the postfix-expression is a dependent name [...]
If the call would [...]

(Here "the call" refers to each call with a dependent name, so this wouldn't apply to foo(my_struct{}), which isn't dependent)

The second case is covered by the one definition rule:

There can be more than one definition of a [...] non-static function template [...] in a program provided that each definition appears in a different translation unit, and the provided definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

  • [...]
  • in each definition of D, corresponding names, looked up according to [basic.lookup], shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution and after matching of partial template specialization ([temp.over]), [...]

Your code should be well defined, as long as in no other translation units the definition for int foo(my_struct) comes before the definition for template<typename T> int do_foo(T t).


As for an example for [temp.dep.candidate], the following two translation units would have undefined behaviour:

template<typename T>
int foo(T) {
  return 1;
}

struct my_struct {};

template<typename T>
int do_foo(T t) {
  return foo(t);
}

int main() {
  return do_foo(my_struct{});
}
struct my_struct {};

int foo(my_struct) {
  return 2;
}

(Since int foo(my_struct) would be a better match but is not found in the template instantiation context)

Upvotes: 2

Related Questions