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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:54:00+00:00 2026-06-02T12:54:00+00:00

Is it O.K. to define virtual function of class template outside its body? Virtual

  • 0

Is it O.K. to define virtual function of class template outside its body? Virtual function can not be inlined, but to avoid multiple definitions in compilation units they shall be marked inline (assuming that template headers will be included in multiple source files). On the other hand compiler is free to ignore inline, so this seems valid. By an example, is the code below correct:

template <typename T>
class C
{
public:
    virtual void f(T val);
};

template <typename T>
inline
void C<T>::f(T val)
{
  //definition
}

?

BTW gcc (3.4.2) allows to omit inline before definition of function f(T val) but not before analogous function of regular class. Is it only gcc’s behaviour?

  • 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-02T12:54:01+00:00Added an answer on June 2, 2026 at 12:54 pm

    Yes, it’s OK even without inline. It works the same for ordinary member functions and static variables:

    // everything in the header:
    template <class T>
    class A
    {
      static int i;
    };
    
    template <class T>
    int A<T>::i=0;
    

    Standard quote: (3.2/5)

    There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with
    external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member
    of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for
    which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition
    appears in a different translation unit, and provided the definitions satisfy the following requirements …

    The requirements basically say the two definitions have to be identical.

    It doesn’t work in case of regular classes. There has to be at most one definition in the whole program.

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

Sidebar

Related Questions

If I derive a class from another one and overwrite a function, I can
I have a class template and I can't seem to figure out how to
I have Foo.hpp and Foo.cpp, i'd like to define a virtual function virtual void
I'm trying to define a base a class in C++ that has pure virtual
I defined next class with virtual properties: public class Order: BaseEPharmObject { public Order()
#define __HAVE_ARCH_STRCPY What's the meaning of __HAVE_ARCH ? I'm not a native speaker and
Possible Duplicate: “Undefined reference to” template class constructor I've just started using templates, and
on more than one occasion I felt the need to define class methods that
I'm trying to use inheritance among classes defined inside a class template (inner classes).
I have two header files A.h(include a pure virtual function) and B.h. A.h :

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.