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?