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 6148923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:21:53+00:00 2026-05-23T19:21:53+00:00

I have a problem with class member function templates. It seems that gcc is

  • 0

I have a problem with class member function templates. It seems that gcc is trying to analyze the code semantically too early, without explicit template instantiation.

Let the code speak

A.h (the base class):

#ifndef __A_H__
#define __A_H__
class A {
public:
    A* ptr;

    template<class T>
    void method() {
        ((B*) ptr)->invoke();
    }
};
#endif 

B.h (the ancestor):

#ifndef __B_H__
#define __B_H__
#include<cstdio>
class B : public A {
        public:
        void invoke() {
                printf("Method invoked\n");
        }
};
#endif 

main.cpp:

#include"A.h"
#include"B.h"

int main() {
        A a;
        a.ptr = new B();
        a.method<int>();
}

This code compiles and runs fine in Visual Studio 2010 and fails to compile with following error on gcc 4.5:

In file included from main.cpp:1:0: A.h: In member function ‘void
A::method()’: A.h:10:5: error: ‘B’ was not declared in this scope
A.h:10:7: error: expected primary-expression before ‘)’ token
A.h:10:9: error: expected ‘)’ before ‘ptr’

Why is gcc trying to compile code of A::method<>() before any instantiation is requested? Which compiler behaves according to the standard?

UPDATE: Now I know that Visual Studio behaves incorrectly. B is not template parameter dependant, so it’s not looked up at instantiation, but during parsing of A.h. (relevant gcc doc)

  • 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-05-23T19:21:54+00:00Added an answer on May 23, 2026 at 7:21 pm

    The code incorrectly compiles in Visual Studio due to the way templates are processed (basically they are ignored until they get instantiated). In your code, the identifier B is not dependent on the template arguments (T) and as such, it must be available in the context where the template is defined.

    As B inherits from A, that makes everything even more complicated, as you have a cyclic dependency in the code. I would reconsider the design, and if you consider that it needs to be so, I would merge the two header files into a single one:

    // merged.h
    struct A {
       A* ptr;
       template<class T>
       void method();
    };
    
    struct B : public A {
       void invoke() {
          printf("Method invoked\n");
       }
    };
    
    template <typename T>
    void A::method() {
       ((B*) ptr)->invoke();
    }
    

    Still, my advice would be to reconsider whether that relantionship makes sense at all, as method imposes the restriction that ptr must be a B, and that rings some bells of code smell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the problem that my view controller class has too many delegates and
i have a problem calling multiple instance of a class that i have coded
I have been writing several class templates that contain nested iterator classes, for which
We have a problem using a class template which itself uses function objects in
I am trying to pass a member function within a class to a function
I have a problem with the following code: class SymmetryTypes { public: enum Type
I have a problem with the following code: template <typename U> class lamePtr {
Somewhere in my code I have: class aclass { ... function amethod() { $this->dom
Good day, I have the following problem: class B extends class A and methods
I have a problem with ViewHolder class. I use ViewHolder to improve my List

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.