Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is object code generated for unused template class methods?

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

like image 222
hsivonen Avatar asked Oct 08 '08 14:10

hsivonen


People also ask

Is an object a template of a class?

The objects created by a particular class template are called the instances or objects of that class. Each instance contains the members specified in the class template.

How do you create a object of template class?

Class Template DeclarationA class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration. template <class T> class className { private: T var; ... .. ... public: T functionName(T arg); ... .. ... };

What is class template in Object Oriented Programming?

Templates are the mechanism by which C++ implements the generic concept. Simply, they allow you to pass data type as a parameter so that you don't need to write the same code for different data types.

Is vector a class template or object template?

vector is a template class, which can be instantiated with a type, in the format: vector<int> , vector<double> , vector<string> .


1 Answers

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.

like image 175
7 revs, 2 users 94% Avatar answered Sep 23 '22 12:09

7 revs, 2 users 94%