From 8d2c079900f1cd1e1179e186fcd96647db489681 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Sat, 31 May 2014 21:53:37 +0400 Subject: [PATCH] template_specialisation.cpp --- template_specialisation.cpp | 119 ++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 template_specialisation.cpp diff --git a/template_specialisation.cpp b/template_specialisation.cpp new file mode 100644 index 0000000..7a6ca84 --- /dev/null +++ b/template_specialisation.cpp @@ -0,0 +1,119 @@ +/* Check cf5-opt.vim defs. +VIM: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:cppflags=g:Iboost.g:Itbb +VIM: let g:ldflags=g:Lboost.g:Ltbb.g:tbbmalloc.g:tbbmproxy +VIM: let g:ldlibpath=g:Bboost.g:Btbb +VIM: let g:argv="" +VIM-: let g:cf5output=0 +*/ +#include +#include + +template +struct A +{ + A() + { + std::cout << "A<" << a << ">" << std::endl; + } +}; + +template <> +struct A<5> +{ + A() + { + std::cout << "special A<5>" << std::endl; + } +}; + +template +struct B +{ + B() + { + std::cout << "B<" << a << ">" << std::endl; + } +}; + +template +struct B +{ + B() + { + std::cout << "special B<5>" << std::endl; + } +}; + +template +void f() +{ + std::cout << "f<" << a << ">()" << std::endl; +} + +template <> +void f<5>() +{ + std::cout << "special f<5>()" << std::endl; +} + +template +void g() +{ + std::cout << "g<" << a << ">()" << std::endl; +} + +/* + * no partial specialisation for functions. +template +void g() +{ + std::cout << "special g<5>()" << std::endl; +} +*/ +template <> +void g() +{ + std::cout << "special g()" << std::endl; +} + +int main ( void ) +{try{ + + A<1> a1; + A<2> a2; + A<5> a5; + A<6> a6; + + B b1; + B b2; + B b5; + B b6; + + f<1>(); + f<2>(); + f<5>(); + f<6>(); + + g(); + g(); + g(); + g(); + g(); + + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} +