c++ - Why Allow ADL lookup for template name binding? -


i know dependent names, binding must deferred point of instantiation. think following code:

#include <iostream>  using namespace std;  template <class t>  void g(t a) {     f(a); }  namespace mynamespace {     class x{};     void f(x) {  cout << "f(x)\n"; } }  void f(int i) { cout << "f(int)\n"; }  int main() {     g(mynamespace::x{});   //1. print f(x)     //g(1);                //2. not compile     return 0; }  

for 1, compilers tested(vc++, gcc, clang) find mynamespace::f(x) adl. that's fine, in accordance standard. however, gcc , clang cannot compile 2. because standard not allow directly searching functions in instantiation context. that's standard requires (fine, vc++ violates standard in respect).

i know standard not allow searching functions in template instantiation context safeguard odr. however, why allow adl examines function declarations visible template instantiation context? cause violation of odr well? consider follow:

//ff.h #include <iostream>  namespace n {     class x {};     int g(x, int i); }  template<typename t> double ff(t t, double d) {     return g(t, d); }  //ff.cpp #include "ff.h"  int n::g(x, int i) { std::cout << "g(x,int)" << std::endl; return i; }  double x1 = ff(n::x{}, 1.1);  //main.cpp #include "ff.h"  namespace n {     double g(x, double d) { std::cout << "g(x,double)" << std::endl; return d; } }  auto x2 = ff(n::x{}, 2.2);  int main() {     extern double x1;     std::cout<<"x1 = " << x1 << std::endl;     std::cout << "x2 = " << x2 << std::endl;     return 0; } 

there 2 places instantiate function template ff: ff(n::x, double), 1 in ff.cpp, 1 in main.cpp. in main.cpp, namespace changed , better matching function double g(x, double) added in main.cpp before instantiation of ff. therefore, instantiation in main.cpp pick better matching functiong, anyway both g equal in scope of main.cpp. instantiation in ff.cpp choose first g declared in ff.h. violates odr, though tested 3 compilers pick int g(x, int), seems compilers smart enough detect illness in generated code , optimized out duplicated specialization. compilers not obliged catch problems of kind. why standard allows adl in case, lead potential violation odr?


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -