14

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)?

hsivonen
  • 7,908
  • 1
  • 30
  • 35

3 Answers3

25

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.

AndyG
  • 39,700
  • 8
  • 109
  • 143
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
  • We should also note that not all function can even be instantiated and this is ok as long as they aren't called. – Matt Price Oct 08 '08 at 15:18
  • Done. I thought about that when initially answering, but was just too lazy to write a full explanation. – Kristopher Johnson Oct 08 '08 at 17:52
  • Is it possible to mark the method to be force-instantiated (i.e. prevent it from being optimized away) without making it virtual? Thanks! – Serge Aug 07 '13 at 14:39
2

I think it depends on the compiler and settings. For example, I believe MSVC6 generated everything, but VS2005 does not. The spec says the compiler should not, but in the real world, it depends on the actual compiler (there are many work-arounds in boost for MSVC6, for example). The linker can remove unreferenced functions if /opt:ref is enabled (for VS, equivalent options exist for other compilers).

Nick
  • 6,808
  • 1
  • 22
  • 34
1

Usually, yes.

All the compiler really knows is that your program can create at least one instance of each class. But it doesn't know what you will do with those instances. So the code will almost certain be generated.

That said, if the methods in question are not virtual, and are never called, the linker can remove them with its normal dead code removal features. So the generated (and compiled) code won't be in the final EXE.

Also this will largely depend on the C++ compiler being used because they're not all the same.

Jeff B
  • 1,856
  • 2
  • 17
  • 28