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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:38:34+00:00 2026-05-10T16:38:34+00:00

I have a C++ template class that gets instantiated with 3 different type parameters.

  • 0

I have a C++ template class that gets instantiated with 3 different type parameters. There’s a method that the class needs to have for only one of those types and that isn’t ever called with the two other types.

Will object code for that method be generated thrice (for all types for which the template is instantiated), or is object code generated only once (for the type with which it is actually used)?

  • 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-10T16:38:35+00:00Added an answer on May 10, 2026 at 4:38 pm

    Virtual member functions are instantiated when a class template is instantiated, but non-virtual member functions are instantiated only if they are called.

    This is covered in [temp.inst] in the C++ standard (In C++11, this is §14.7.1/10. In C++14, it is §14.7.1/11, and in C++17 it is §17.7.1/9. Excerpt from C++17 below)

    An implementation shall not implicitly instantiate a function template, a variable template, a member template, a non-virtual member function, a member class, a static data member of a class template, or a substatement of a constexpr if statement (9.4.1), unless such instantiation is required

    Also note that it is possible to instantiate a class template even if some of the member functions are not instantiable for the given template parameters. For example:

    template <class T> class Xyzzy { public:     void CallFoo() { t.foo(); }  // Invoke T::foo()     void CallBar() { t.bar(); }  // Invoke T::bar()  private:     T t; };  class FooBar { public:     void foo() { ... }     void bar() { ... } };  class BarOnly { public:     void bar() { ... } };  int main(int argc, const char** argv) {     Xyzzy<FooBar>  foobar;    // Xyzzy<FooBar> is instantiated     Xyzzy<BarOnly> baronly;   // Xyzzy<BarOnly> is instantiated      foobar.CallFoo();         // Calls FooBar::foo()     foobar.CallBar();         // Calls FooBar::bar()      baronly.CallBar();        // Calls BarOnly::bar()      return 0; } 

    This is valid, even though Xyzzy::CallFoo() is not instantiable because there is no such thing as BarOnly::foo(). This feature is used often as a template metaprogramming tool.

    Note, however, that ‘instantiation’ of a template does not directly correlate to how much object code gets generated. That will depend upon your compiler/linker implementation.

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

Sidebar

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • 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 Since you ask for .net 2.0 i guess you know… May 11, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer As andri pointed out RXTX is pretty much your best… May 11, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer insert parent (userId, group, name) select distinct userId, group, concat("user… May 11, 2026 at 5:46 pm

Related Questions

I would like to create in C++ a Notifier class that I will use
I'd like to know some best practice when designing c++ classes. To put it
I have a template class where I want to use objects of that class
I have a vector-like class that contains an array of objects of type T

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.