Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) TemplateClassExample6

 

STLQt CreatorWindows

 

Template class example 6: copy policy of template class type, two 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: ./CppTemplateClassExample6/CppTemplateClassExample6.pro

 

include(../../ConsoleApplication.pri)
include(../../Libraries/Boost.pri)
SOURCES += main.cpp

 

 

 

 

 

./CppTemplateClassExample6/main.cpp

 

enum class CopyPolicy { allow, forbid };

template <class T, CopyPolicy copy_policy = CopyPolicy::allow> struct MyClass;

template <class T>
struct MyClass<T,CopyPolicy::forbid>
{
  MyClass(const T& x) : m_x(x) {}
  MyClass<T,CopyPolicy::forbid>(const MyClass<T,CopyPolicy::forbid>&) = delete;
  MyClass<T,CopyPolicy::forbid>& operator=(const MyClass<T,CopyPolicy::forbid>&) = delete;
  private:
  T m_x;
};

template <class T>
struct MyClass<T,CopyPolicy::allow>
{
  MyClass(const T& x) : m_x(x) {}
  private:
  T m_x;
};

int main()
{
  const MyClass<int,CopyPolicy::allow> a(123);
  const MyClass<int,CopyPolicy::forbid> b(123);

  const MyClass<int> c(a);
  //const MyClass<int,IntCopyPolicy::forbid> d(b); //Won't 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