Template class with invalid member functions

那年仲夏 提交于 2019-12-08 03:52:45

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/30290231/template-class-with-invalid-member-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!