Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is C++ name lookup doing here? (& is GCC right?)

I was having a problem in some production code that I minimized to the following test case:

template<typename T>
void intermediate(T t)
{
    func(t); // line 4 ("func not declared in this scope")
}

namespace ns {
    struct type {};
}

void func(ns::type const & p); // line 11 ("declared here, later")

void foo(ns::type exit_node)
{
    intermediate(exit_node);  // line 15 ("required from here")
}

GCC 4.5 compiles this fine. Both with and without -std=c++11, 4.7 and 4.9 produce messages like:

test.cpp: In instantiation of ‘void intermediate(T) [with T = ns::type]’:
test.cpp:15:27:   required from here
test.cpp:4:5: error: ‘func’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
test.cpp:11:6: note: ‘void func(const ns::type&)’ declared here, later in the translation unit

All of the following three things will cause the file to successfully compile:

  1. Move func(ns::type) into the ns namespace (allowing ADL to find it in ns)
  2. Move type into the global namespace (allowing ADL to find it in ::)
  3. Get rid of intermediate and call func directly from foo

So... what is going on here? Is it legal for GCC to reject this program? Why is func found by unqualified lookup in the third variant (call func directly from foo) but not found by unqualified lookup in the original variant at the point of instantiation?

like image 436
EvanED Avatar asked Apr 08 '15 19:04

EvanED


People also ask

How does CNAME lookup work?

Each CNAME record associates a service with a domain name, not a physical IP address. The physical IP address is instead identified by your domain's A record. If your IP address changes, you only have to change the A record, not each CNAME record.

What is CNAME with example?

CNAME records can be used to alias one name to another. CNAME stands for Canonical Name. A common example is when you have both example.com and www.example.com pointing to the same application and hosted by the same server.

What is the benefit of CNAME?

The major advantage of using CNAME is that if we change the IP address of one A record then any CNAME record pointing to that host will also changed. Like if we have a website with highcloud.com then this domain name is hooked up to an A-record which translates the domain name to the appropriate IP address 1.2.


1 Answers

The general rule is that anything that is not in the template definition context can only be picked up via ADL. In other words, normal unqualified lookup is performed only in the template definition context.

Since no declaration of func is visible when intermediate was defined, and func is not in a namespace associated with ns::type, the code is ill-formed.

like image 88
T.C. Avatar answered Sep 20 '22 17:09

T.C.