October 24, 2024
Chicago 12, Melborne City, USA
templates

Argument-dependent lookup for built in types vs user-defined types


My code:

template <typename T>
void func(T v)
{
    func2(v);
}
    
struct S {};
    
void func2(int) {}
void func2(S) {}
    
int main()
{
    func(1);  // error here
    func(S{}); // OK
}

The error:

<source>: In instantiation of 'void func(T) [with T = int]':
<source>:14:9:   required from here
   14 |     func(1);  // error here
      |     ~~~~^~~
<source>:4:10: error: 'func2' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
    4 |     func2(v);
      |     ~~~~~^~~
<source>:10:6: note: 'void func2(S)' declared here, later in the translation unit
   10 | void func2(S) {}
      | 

Why does the lookup fail for func2 with built in types but not user-defiened?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video