Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) TemplateClassExample5

 

STLQt CreatorWindows

 

Template class example 5: copy policy of integer class 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: ./CppTemplateClassExample5/CppTemplateClassExample5.pro

 

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

 

 

 

 

 

./CppTemplateClassExample5/main.cpp

 

enum class IntCopyPolicy { allow, forbid };

template <IntCopyPolicy copy_policy> struct Int;

template <>
struct Int<IntCopyPolicy::forbid>
{
  Int(const int x) : m_x(x) {}
  Int<IntCopyPolicy::forbid>(const Int<IntCopyPolicy::forbid>&) = delete;
  Int<IntCopyPolicy::forbid>& operator=(const Int<IntCopyPolicy::forbid>&) = delete;
  private:
  int m_x;
};

template <>
struct Int<IntCopyPolicy::allow>
{
  Int(const int x) : m_x(x) {}
  private:
  int m_x;
};

int main()
{
  const Int<IntCopyPolicy::allow> a(123);
  const Int<IntCopyPolicy::forbid> b(123);

  const Int<IntCopyPolicy::allow> c(a);
  //const 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