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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:25:47+00:00 2026-05-11T13:25:47+00:00

In C++, a function template specialization is supposed to act exactly like a normal

  • 0

In C++, a function template specialization is supposed to act exactly like a normal function. Does that mean that I can make one virtual?

For example:

struct A {     template <class T> void f();     template <> virtual void f<int>() {} };  struct B : A {     template <class T> void f();     template <> virtual void f<int>() {} };  int main(int argc, char* argv[]) {     B b;     A& a = b;     a.f<int>(); } 

Visual Studio 2005 gives me the following error:

fatal error C1001: An internal error has occurred in the compiler.

  • 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. 2026-05-11T13:25:47+00:00Added an answer on May 11, 2026 at 1:25 pm

    Nice compiler error. For this type of checks I always fallback to the Comeau compiler before going back to the standard and checking.

    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2 Copyright 1988-2008 Comeau Computing. All rights reserved. MODE:strict errors C++ C++0x_extensions

    ‘ComeauTest.c’, line 3: error: ‘virtual’ is not allowed in a function template declaration template virtual void f(); ^

    ‘ComeauTest.c’, line 10: error: ‘virtual’ is not allowed in a function template declaration template virtual void f(); ^

    Now, as it has been posted by another user, the fact is that the standard does not allow you to define virtual templated methods. The rationale is that for all virtual methods, an entry must be reserved in the vtable. The problem is that template methods will only be defined when they have been instantiated (used). This means that the vtable would end up having a different number of elements in each compilation unit, depending on how many different calls to f() with different types happen. Then hell would be raised…

    If what you want is a templated function on one of its arguments and one specific version being virtual (note the part of the argument) you can do it:

    class Base { public:    template <typename T> void f( T a ) {}    virtual void f( int a ) { std::cout << 'base' << std::endl; } }; class Derived : public Base { public:    virtual void f( int a ) { std::cout << 'derived' << std::endl; } }; int main() {    Derived d;    Base& b = d;    b.f( 5 ); // The compiler will prefer the non-templated method and print 'derived' } 

    If you want this generalized for any type, then you are out of luck. Consider another type of delegation instead of polymorphism (aggregation + delegation could be a solution). More information on the problem at hand would help in determining a solution.

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

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Don't use WebClient for this, but rather sockets(if possible.) Mike… May 11, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer Here are some ways to do it: grep --color 'pattern\|$'… May 11, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer Ok, there are alot of questions there, I'll try to… May 11, 2026 at 8:04 pm

Related Questions

I am hosting SpiderMonkey in a current project and would like to have template
Explain about linkages(external/internal) in c++? How does linkage differ for a function,constant,inline function,template function
Possible Duplicate: What is the difference between "typename" and "class" template parameters? When defining
I am trying to use std::for_each to output the contents of vectors, which may

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.