Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:11:49+00:00 2026-06-14T02:11:49+00:00

Consider the following C++ code example: namespace n { struct A {}; } struct

  • 0

Consider the following C++ code example:

namespace n
{
    struct A {};
}

struct B {};

void foo(int) {}

template<typename T>
void quux()
{
    foo(T());
}

void foo(n::A) {}
void foo(B) {}


int main()
{
    quux<n::A>(); // Error (but works if you comment out the foo(int) declaration)
    quux<B>();    // Works

    return 0;
}

As indicated in the comment, the template instantiation quux<n::A>() causes a compiler error (on GCC 4.6.3):

foo.cpp: In function ‘void quux() [with T = n::A]’:
foo.cpp:22:16:   instantiated from here
foo.cpp:13:5: error: cannot convert ‘n::A’ to ‘int’ for argument ‘1’ to ‘void foo(int)’

Can someone explain to me what is going on? I would have expected for it to work the same as with quux<B>(). It must have something to do with when foo is considered dependent. Unfortunately my C++ foo is not good enough. The example compiles fine, when the foo(int) declaration is not present, which is also surprising to me.

Any hints, explanations and workarounds are welcome.

Update 1:

I do not want to (read cannot) move the declaration of foo(n::A) before the definition of quux (which would avoid the error).

Update 2:

Thanks for David for pointing out the related question Template function call confused by function with wrong signature declared before template. The accepted answer by Johannes Schaub – litb proposes a wrapper class solution, that would also work in my case as a workaround. However, I’m not 100% happy with it.

Update 3:

I solved the issue by putting the definition of foo(n::A) in namespace n. Thanks for the helpful answers of Jesse Good and bames53 that not only point out the relevant sections of the standard, but also provide alternative solutions. Thanks to David Rodríguez – dribeas for his explanation when I did not understand the proposed solutions properly and all other contributors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:11:50+00:00Added an answer on June 14, 2026 at 2:11 am

    I think the rule is 14.6.4.2p1:

    For a function call that depends on a template parameter, the
    candidate functions are found using the usual lookup rules (3.4.1,
    3.4.2, 3.4.3) except that:

    — For the part of the lookup using unqualified name lookup (3.4.1) or qualified name lookup (3.4.3), only
    function declarations from the template definition context are found.

    — For the part of the lookup using associated namespaces (3.4.2), only
    function declarations found in either the template definition context
    or the template instantiation context are found.

    void foo(n::A) {} is not visible in the template definition context because it comes after and foo is not in the same namespace as n::A. So it needs to be either visible before the template definition or included in the same namespace like below:

    namespace n
    {
        void foo(n::A) {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code template<typename T, int N> struct A { typedef T value_type;
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code in C: void main() { int a=0; for(printf(\nA); a; printf(\nB));
Consider the following code: int SomeField; void Foo([Pure] Func<int, object> getData) { Contract.Requires(getData !=
Consider following example code: #include <iostream> #include <inttypes.h> using namespace std; int f(uint32_t i)
Consider the following code: template<size_t head,size_t ... Dims> struct make_vec { typedef typename make_vec<Dims...>::type
Consider the following short code snippet. namespace B { public class Foo { public
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following C++ code example: int array_1[] = {5,6,7}; int array_2[] = {6,7,8,9};
Consider the following code: void f(byte x) {print(byte);} void f(short x) {print(short);} void f(int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.