2

Is it legal in C++ to have instantiate class templates with classes that do not work with some of its member functions?

For example:

class A {
public:
    void f() { }
};

class B {  
};

template<typename T>
class Wrapper {
private:
    T t_;
public:
    void call_f() { t_.f(); }
};

int main() {
    Wrapper<A> a;
    Wrapper<B> b;
    a.call_f();
}

This code compiles, and I can use b, as long as I don't try to call b.call_f(). (Also explicitly instantiating it with template class Wrapper<B>; causes a compilation error because that instantiates all member functions.)

Is this guaranteed to work or is it undefined behavior? If so, will this change in C++17 with the introduction of concepts and requirements?

Hiura
  • 3,500
  • 2
  • 20
  • 39
tmlen
  • 8,533
  • 5
  • 31
  • 84
  • I think you clearly understand the problem you have here, it'll work since this issues are all resolved at compile time - not run time. Therefore - if you do decide to do "b.call_f()" - it'll be caught at compile time. I don't see any conflict here. I can't comment about C++17 – stackmate May 17 '15 at 18:05
  • This has always worked and why wouldn't it? C++17 will try to remain as compatible as possible, changing something basic like that would be madness! – Daniel Frey May 17 '15 at 18:06

2 Answers2

7

Yes, in general. Non-virtual member functions of class templates are themselves function templates, and like all function templates they only get instantiated when used. So if you never use some member function of a class template specialization, member function need not be valid for that specialization.

Since C++11, the standard library actually makes ample use of this fine-grained instantiation control. Type requirements for containers apply to member functions, not to the entire template, so for example you can have an std::map<K, T> where T is not default-constructible; you just cannot call operator[] on it.

Note that explicit class template instantiation instantiates all the member functions.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

This is standard behavior and will not change.

The reason is the following: Templates are generic code that needs to work with various type arguments. Some operations in templated code may be perfectly valid for one type (as call_f on A), but horribly wrong for another (as call_f on B). The decision made in the standard was to allow non-sensical template code, such as the call_f for type B, as long as this template function is never used (which would trigger compilation of the template function).

That way, code can be generic and also safe, because these checks are done at compile time.

user1978011
  • 3,419
  • 25
  • 38