Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) TemplateClassExample7

 

STLQt CreatorWindows

 

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)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

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
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml