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

  • Home
  • SEARCH
  • 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 7753213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:59:56+00:00 2026-06-01T11:59:56+00:00

struct A { void f(int x) {} }; struct B { template<typename T> void

  • 0
struct A {
    void f(int x) {}
};

struct B {
    template<typename T> void f(T x) {}
};

struct C : public A, public B {};

struct D {
    void f(int x){}
    template<typename T> void f(T x) {} 
};


int main(int argc, char **argv) {
    C c;
    c.f<int>(3);
    D d;
    d.f<int>(3);
}

What is the reason for which calling d.f is fine, but c.f gives

error: request for member ‘f’ is ambiguous
error: candidates are: template<class T> void B::f(T)
error:                 void A::f(int)
  • 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-01T11:59:57+00:00Added an answer on June 1, 2026 at 11:59 am

    The first part is due to member name lookup, that’s why it fails.

    I would refer you to: 10.2/2 Member name lookup

    The following steps define the result of name lookup in a class scope,
    C. First, every declaration for the name in the class and in each of
    its base class sub-objects is considered. A member name f in one
    sub-object B hides a member name f in a sub-object A if A is a base
    class sub-object of B. Any declarations that are so hidden are
    eliminated from consideration. Each of these declarations that was
    introduced by a using-declaration is considered to be from each
    sub-object of C that is of the type containing the declaration
    designated by the using-declaration.

    If the resulting set of declarations are not all from sub-objects of
    the same type, or the set has a nonstatic member and includes members
    from distinct sub-objects, there is an ambiguity and the program is
    ill-formed
    . Otherwise that set is the result of the lookup.

    Now, for the matter with template functions.

    As per 13.3.1/7 Candidate functions and argument list

    In each case where a candidate is a function template, candidate
    function template specializations are generated using template
    argument deduction (14.8.3, 14.8.2). Those candidates are then handled
    as candidate functions in the usual way. A given name can refer to one
    or more function templates and also to a set of overloaded
    non-template functions. In such a case, the candidate functions
    generated from each function template are combined with the set of
    non-template candidate functions.

    And if you continue reading 13.3.3/1 Best viable function

    F1 is considered to be a better function, if:

    F1 is a non-template function and F2 is a function template
    specialization

    That’s why the following snippet compiles and runs the non-template function without error:

    D c;
    c.f(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: template<class T> struct test { void foo(int b); void testFunc( int a )
Suppose I have a static function template template<int I> void ft() within a struct
Short story: struct A{}; struct B:private A{}; void f(void *){} void f(A*){} int main(){
struct GPattern() { int gid; .... } class Example() { public: void run(string _filename,
I've got an extremely basic allocator: template<typename T> struct Allocator : public std::allocator<T> {
Consider something like: struct Parameter { int a; Parameter(){a = 0;} void setA(int newA){a
struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
Given two structure in c: typedef struct _X_ { int virtual_a; int virtual_b; void
I'm so sick of the pass-callback-data-as-void*-struct anti-pattern. Boost bind solves it nicely, but is

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.