(C++) TemplateClassExample7
February 24, 2017 · View on GitHub
(C++) TemplateClassExample7



Template class example 7: copy policy of template class type, two partially specialized classes is a template class example.
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version
4.9.2
Qt project file: ./CppTemplateClassExample7/CppTemplateClassExample7.pro
include(../../ConsoleApplication.pri) include(../../Libraries/Boost.pri) SOURCES += main.cpp
./CppTemplateClassExample7/main.cpp
//Does not compile // //Unknown how to compile this enum class CopyPolicy { allow, forbid }; template <class T, CopyPolicy copy_policy> struct MyClass { //All MyClass template classes must have a same constructor MyClass<T,copy_policy>(const T& x) : m_x(x) {} private: T m_x; }; template <class T> struct MyClass<T,CopyPolicy::forbid> { MyClass<T,CopyPolicy::forbid>(const MyClass<T,CopyPolicy::forbid>&) = delete; MyClass<T,CopyPolicy::forbid>& operator=(const MyClass<T,CopyPolicy::forbid>&) = delete; }; template <class T> struct MyClass<T,CopyPolicy::allow> { }; int main() { const MyClass<int,CopyPolicy::allow > a(123); //Use general constructor: Does not compile const MyClass<int,CopyPolicy::forbid> b(123); //Use general constructor: Does not compile const decltype(a) c(a); //Should compile const decltype(b) d(b); //Should not compile }