Overload template member function over any container of A or B - c++

I have 2 existing classes A and B. I want to implement a third struct C such that C implement operator() which would take any container of A or using a different implementation any container of B. Is it possible to do a such thing using template specialization ?

enable_if is very useful in lots of scenarios, but in cases like these I'm usually more inclined to use tag dispatching. To my eye, the code looks cleaner, behaves more predictably, and if you try to use it wrongly, the error messages make slightly more sense.
struct C
{
template <class T>
void operator()(const T& container) const
{
operator()(container, Tag<typename T::value_type>());
}
private:
template <class V> struct Tag {};
template <class T>
void operator()(const T& container, Tag<A>) const
{
std::cout << "A\n";
}
template <class T>
void operator()(const T& container, Tag<B>) const
{
std::cout << "B\n";
}
};
int main()
{
std::vector<A> a;
std::list<B> b;
C c;
c(a);
c(b);
}

You can do this using template template parameters.
Assuming your containers have two parameters (content type and allocator):
class A {};
class B {};
struct C {
template <
template <
typename,
typename
>
class V
>
void operator()(const V<A, std::allocator<A> >& /* ca */) const {
}
template <
template <
typename,
typename
>
class V
>
void operator()(const V<B, std::allocator<B> >& /* cb */) const {
}
};
Which would then allow you to do the following:
int main() {
std::vector<A> va;
std::vector<B> vb;
std::list<A> la;
std::list<B> lb;
C c;
c(va);
c(la);
c(vb);
c(lb);
}

Overloads using std::enable_if and std::is_same seem to work. Not very pretty though. Note that these are c++11 features.
struct A {};
struct B {};
struct C {
template<class Cont>
void operator()(const Cont& c, typename std::enable_if<std::is_same<typename Cont::value_type, A>::value>::type * = NULL) const {
std::cout << "a";
}
template<class Cont>
void operator()(const Cont& c, typename std::enable_if<std::is_same<typename Cont::value_type, B>::value>::type * = NULL) const {
std::cout << "b";
}
};
int main() {
std::vector<A> a;
std::list<B> b;
C c;
c(a);
c(b);
}

Yes it should be possible. Arkanosis has a good solution but here is if you want to use template specialization:
class A {};
class B {};
template <template <typename , typename > class Z, class Elem> struct C
{
void operator()(const Z<Elem, std::allocator<Elem>>& v) { std::cout << "General implement" << std::endl; }
};
template <template <typename, typename > class Z> struct C<Z,B>
{
void operator()(const Z<B, std::allocator<B>>& v) { std::cout << "Specialized implement" << std::endl; }
};
Then some client code:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
std::vector<A> as;
std::list<B> bs;
C<vector,A> ca;
ca(as);
C<list,B> cb;
cb(bs);
}
If you want to handle different container types from the same C-style class you can derive from templates of the C struct e.g. class MultiContainerHandlerForA : public C<vector,A>, public C<list,A>.

Related

test class type without specifying template argument in c++20 [duplicate]

This question already has answers here:
How can I define a concept that is satisfied by an arbitrary std::vector?
(3 answers)
Closed last year.
I would like to select instance by type while iterating via std::apply in a std::tuple.
Since I have multiple times the same class in my tuple I can not just use std::get
Is there a way to express that I want all the variant of A instead of writting
A< ... >
#include <iostream>
#include <tuple>
template <typename T>
struct A
{
public:
T val;
A(T t) : val(t)
{};
};
using MyType = std::tuple< A<double> , A<double> , A<int>, bool>;
template<typename T>
void logic(T& c)
{
if constexpr ( std::is_same<T, A< ... > >::value)
std::cout << c.val << std::endl;
}
void printOnlyA(MyType& v)
{
std::apply([&](auto &&...c) { ( logic(c), ...); }, v);
}
int main() {
A<double> a{3.5};
A<double> b{5.5};
A<int> c{2};
MyType dd{std::make_tuple(a,b,c , false)};
printOnlyA(dd);
return 0;
}
I m using c++ 20, and would like to avoid the old c++11 approach to std::false_type and std::true_type. I can not figure out the way to do it with concept
Thanks in advance
You can use partial template specialization. Since partial specialization for functions is not a part of the language a dedicated class template is required:
#include <iostream>
#include <tuple>
template <typename T>
struct A
{
public:
T val;
A(T t) : val(t)
{};
};
using MyType = std::tuple< A<double> , A<double> , A<int>, bool>;
template<typename T>
struct logic_impl
{
static void exec([[maybe_unused]] T & c)
{
// nothing
}
};
template<typename T>
struct logic_impl<A<T>>
{
static void exec(A<T> & c)
{
std::cout << c.val << std::endl;
}
};
template<typename T>
void logic(T& c)
{
logic_impl<T>::exec(c);
}
void printOnlyA(MyType& v)
{
std::apply([&](auto &&... c) { (logic(c), ...); }, v);
}
int main() {
A<double> a{3.5};
A<double> b{5.5};
A<int> c{2};
MyType dd{std::make_tuple(a,b,c , false)};
printOnlyA(dd);
return 0;
}
online compiler

How to delegate traits functionality to member of class?

I'm searching for a way to delegate trait functionality to member. Inheritance is not an option at this point.
So assume we have a class B and 2 different Traits already working with that class.
struct B
{};
template<typename T>
struct Trait1
{
static
void foo1(T t)
{}
};
template<>
struct Trait1<B>
{
static
void foo1(B t)
{}
};
template<typename T>
struct Trait2
{
static
void foo2(T t)
{}
};
template<>
struct Trait2<B>
{
static
void foo2(B t)
{}
};
I also have an aggregate class C with 2 members of that class B, like:
struct C
{
B m1;
B m2;
};
Now I want to define the both Traits for that class C as well, with delegating to the appropriate member. The plain approach would be s.th. like:
template<>
struct Trait1<C>
{
static
void foo1(C t)
{
Trait1<B>::foo1(t.m1);
}
};
template<>
struct Trait2<C>
{
static
void foo2(C t)
{
Trait2<B>::foo2(t.m2);
}
};
For traits with a lot of functions that is kind of annoying and probably has copy-paste errors. So the question arised to me, is there a way to delegate the functionality in an elegant way (C++11 preferred, C++14/17 also possible)? Meaning In case of Trait1 use member m1 and for Trait2 use member m2.
Thanks for help.
EDIT: methods of the Trait1 and Trait2 have actually different names.
And in the wild a macro replacing 1 with 2 will not work, so I would encourage not to use macros.
You can create a generic traits for factorize code
template<template <typename> class Trait, typename T, typename T2, T2 (T::*M)>
struct GenTrait
{
static
void foo(T t)
{
Trait<T2>::foo(t.*M);
}
};
And then
template<>
struct Trait1<C> : GenTrait<Trait1, C, B, &C::m1> {};
template<>
struct Trait2<C> : GenTrait<Trait2, C, B, &C::m2> {};
First, I needed executor template functions. These are the ones to do perform whatever has to be done and to be specialised or overloaded for other classes:
template<typename T>
void theFoo1(T t)
{
std::cout << "foo 1" << std::endl;
}
template<typename T>
void theFoo2(T t)
{
std::cout << "foo 2" << std::endl;
}
struct B
{};
void theFoo1(B t)
{
std::cout << "foo 1 B" << std::endl;
}
void theFoo2(B t)
{
std::cout << "foo 2 B" << std::endl;
}
Then, I need getter templates that provide the appropriate data:
template<typename T>
inline T get1(T t)
{
return t;
}
template<typename T>
inline T get2(T t)
{
return t;
}
My traits no look like this; note: you won't specialise these any more at any time:
template<typename T>
struct Trait1
{
static inline
void foo(T t)
{
theFoo1(get1(t));
}
};
template<typename T>
struct Trait2
{
static inline
void foo(T t)
{
theFoo2(get2(t));
}
};
Now C comes into play; B is already ready for use, so we can simply continue with C itself; all I need is two additional overloads:
struct C
{
B m1;
B m2;
};
B get1(C t)
{
return t.m1;
}
B get2(C t)
{
return t.m2;
}
Finally: test it...
int main(int, char*[])
{
B b;
Trait2<B>::foo(b);
C c;
Trait1<C>::foo(c);
Trait2<C>::foo(c);
}
Due to your specific requirement (where you need m1 & m2), there is no way to avoid re-writing lot of code for class C separately. To avoid such copy pasting, you may use simple macros. Pseudo code:
#define TRAIT_C(N, FUNCTION) \
template<> \
struct Trait##N<C> \
{ \
static \
void foo(C t) \
{ \
Trait##N<B>::foo(t.m##N); \
} \
}
Now use this in a "eye-soothing" way while defining trait for B:
template<>
struct Trait1<B>
{
static
void foo(B t)
{}
};
TRAIT_C(1, foo); // This inlines the complete version of `Trait1` for `C`
Here is a demo-ideone or demo-coliru.
You can expose the pointers to data members as part of the traits itself.
It can be done by means of template variables (named data in the example below).
It follows a minimal, working example:
struct B {};
template<typename T>
struct Trait1;
template<>
struct Trait1<B>
{
template<typename U>
static constexpr B U::* data = &U::m1;
static
void foo(B t)
{}
};
template<typename T>
struct Trait2;
template<>
struct Trait2<B>
{
template<typename U>
static constexpr B U::* data = &U::m2;
static
void foo(B t)
{}
};
struct C
{
B m1;
B m2;
};
template<>
struct Trait1<C>
{
using BT = Trait1<B>;
static constexpr B C::* data = BT::data<C>;
static
void foo(C t)
{
BT::foo(t.*data);
}
};
template<>
struct Trait2<C>
{
static
void foo(C t)
{
Trait2<B>::foo(t.*Trait2<B>::data<C>);
}
};
In the example above, Trait1<C> and Trait2<C> differ in order to show two possible solutions (I would use the first one if I had the same problem).
After inspiration by some of the proposals (e.g. #Aconcagua ) I thought of a DelegateTrait itself, like:
#include <iostream>
template<typename TraitT, typename DelegateT>
struct DelegateTrait
{
typedef DelegateT MemberT;// type of member delegated to
// method for delegation to specific member
static
MemberT forward(DelegateT delegate)
{
return delegate;
}
};
Now a small adaption of the original Trait1 is needed
template<typename T, typename DelegateT = void>
struct Trait1
{
static
void foo1(T t)
{
std::cout << "default Trait1<T>::foo1()" << std::endl;
}
};
template<typename T>
struct Trait1<
T,
typename std::enable_if<!std::is_same<T, typename DelegateTrait<Trait1<T>, T>::MemberT>::value>::type>
{
static
void foo1(T t)
{
std::cout << "delegate Trait1<T>::foo1()" << std::endl;
Trait1<typename DelegateTrait<Trait1<T>, T>::MemberT>::foo1(DelegateTrait<Trait1<T>, T>::forward(t));
}
};
template<>
struct Trait1<B>
{
static
void foo1(B t)
{
std::cout << "Trait1<B>::foo1()" << std::endl;
}
};
In a similar way an adaption of Trait2 is done. The std::enable_if is used to avoid recursive (and endless) calling of the same method.
You now have to specialize the DelegateTrait for each combination of class and trait where you want to forward to s.th. else but the instance of the given class.
template<>
struct DelegateTrait<Trait1<C>, C>
{
typedef B MemberT;
static
MemberT forward(C delegate)
{
std::cout << "forward C to C.m1" << std::endl;
return delegate.m1;
}
};
template<>
struct DelegateTrait<Trait2<C>, C>
{
typedef B MemberT;
static
MemberT forward(C delegate)
{
std::cout << "forward C to C.m2" << std::endl;
return delegate.m2;
}
};
You can still specialize for the class itself if you don't want to forward, like:
struct D
{
D(){};
B m1;
B m2;
};
template<>
struct Trait2<D>
{
static
void foo2(D t)
{
std::cout << "Trait<D>::foo2(), no delegate" << std::endl;
}
};
int
main(
int argc,
char ** argv)
{
C c;
Trait1<C>::foo1(c);//delegates to m1
Trait2<C>::foo2(c);//delegates to m2
D d;
Trait1<D>::foo1(d);//default trait implementation
Trait2<D>::foo2(d);//trait specialization for D
return 0;
}
Of course that solution needs a extension of the original trait. Do you see some other issue coming with that approach, e.g. performance?

boost::variant and polymorphism

I want to get pointer to base class from boost variant, if I put orignally pointer to derived class. Is there some way to achive this . The following code does not work.
class A{ public: virtual ~A(){}}; class B : public A{};
typedef boost::variant<A*,B*> MyVar;
MyVar var = new B;
A* a = boost::get<A*> (var); // the following line throws exception
Maybe someone have idea how to write my own get function which will test if the requested type is base class of the stored type of in the variant,and then do the appropriate cast
You can write your own visitor with templated operator() like below:
LIVE DEMO
#include <iostream>
#include <boost/variant.hpp>
#include <type_traits>
struct A { virtual ~A() {} virtual void foo() {} };
struct B : A { virtual void foo() { std::cout << "B::foo()" << std::endl; } };
template <typename T>
struct visitor : boost::static_visitor<T>
{
private:
using Base = typename std::remove_pointer<
typename std::remove_cv<
typename std::remove_reference<T>::type
>::type
>::type;
template <typename U>
T get(U& u, std::true_type) const
{
return u;
}
template <typename U>
T get(U& u, std::false_type) const
{
throw boost::bad_get{};
}
public:
template <typename U>
T operator()(U& u) const
{
using Derived = typename std::remove_pointer<
typename std::remove_cv<
typename std::remove_reference<U>::type
>::type
>::type;
using tag = std::integral_constant<bool
, (std::is_base_of<Base, Derived>::value
|| std::is_same<Base, Derived>::value)
&& std::is_convertible<U, T>::value>;
return get(u, tag{});
}
};
template <typename T, typename... Args>
T my_get(boost::variant<Args...>& var)
{
return boost::apply_visitor(visitor<T>{}, var);
}
int main()
{
boost::variant<A*,B*> var = new B;
A* a = my_get<A*>(var); // works!
a->foo();
B* b = my_get<B*>(var); // works!
b->foo();
}
Output:
B::foo()
B::foo()
Q & A section:
This solution is weird!
No, it is not. This is exactly what the visitor classes in Boost.Variant are for. Similar solution already exists in latest release of Boost.Variant, which is boost::polymorphic_get<T>. Sadly it was designed for other purposes and cannot be used here.
Hi thank you all for your answers and comments
I came to the following which decides at compile time if types are inherited from each other. And it seems to work, and it seems much easier to me to understand.
#include <iostream>
#include <boost/variant.hpp>
#include <boost/type_traits.hpp>
#include <boost/utility.hpp>
using namespace boost::type_traits;
struct A { virtual ~A() {} virtual void foo() {} };
struct B : A { virtual void foo() { std::cout << "B::foo()" << std::endl; } };
typedef boost::variant<B*,A*,C*> MyVar;
template <typename A,typename B>
struct types_are_inheritance_related
{
static const bool value=
ice_or<
boost::is_base_of<A, B>::value,
boost::is_base_of<B, A>::value
>::value;
};
template<class Base>
class get_visitor
: public boost::static_visitor<Base*> { public:
template<class T>
Base* operator()( T* t, typename boost::enable_if<types_are_inheritance_related<Base,T> >::type* dummy = 0)
{
Base* b = dynamic_cast<Base*> ( t);
return b;
}
template<class T>
Base* operator()( T* t, typename boost::disable_if<types_are_inheritance_related<Base,T> >::type* dummy = 0)
{
return 0;
}
};
template<class T>
T* get_var_value(MyVar& var)
{
get_visitor<T> visitor;
T* aa= var.apply_visitor(visitor);
return aa;
}
int main()
{
MyVar var = new B;
A* a = get_var_value<A*>(var); // works!
a->foo();
B* b = get_var_value<B*>(var); // works!
b->foo();
}

c++ template specialization for all subclasses

I need to create a template function like this:
template<typename T>
void foo(T a)
{
if (T is a subclass of class Bar)
do this
else
do something else
}
I can also imagine doing it using template specialization ... but I have never seen a template specialization for all subclasses of a superclass. I don't want to repeat specialization code for each subclass
You can do what you want but not how you are trying to do it! You can use std::enable_if together with std::is_base_of:
#include <iostream>
#include <utility>
#include <type_traits>
struct Bar { virtual ~Bar() {} };
struct Foo: Bar {};
struct Faz {};
template <typename T>
typename std::enable_if<std::is_base_of<Bar, T>::value>::type
foo(char const* type, T) {
std::cout << type << " is derived from Bar\n";
}
template <typename T>
typename std::enable_if<!std::is_base_of<Bar, T>::value>::type
foo(char const* type, T) {
std::cout << type << " is NOT derived from Bar\n";
}
int main()
{
foo("Foo", Foo());
foo("Faz", Faz());
}
Since this stuff gets more wide-spread, people have discussed having some sort of static if but so far it hasn't come into existance.
Both std::enable_if and std::is_base_of (declared in <type_traits>) are new in C++2011. If you need to compile with a C++2003 compiler you can either use their implementation from Boost (you need to change the namespace to boost and include "boost/utility.hpp" and "boost/enable_if.hpp" instead of the respective standard headers). Alternatively, if you can't use Boost, both of these class template can be implemented quite easily.
I would use std::is_base_of along with local class as :
#include <type_traits> //you must include this: C++11 solution!
template<typename T>
void foo(T a)
{
struct local
{
static void do_work(T & a, std::true_type const &)
{
//T is derived from Bar
}
static void do_work(T & a, std::false_type const &)
{
//T is not derived from Bar
}
};
local::do_work(a, std::is_base_of<Bar,T>());
}
Please note that std::is_base_of derives from std::integral_constant, so an object of former type can implicitly be converted into an object of latter type, which means std::is_base_of<Bar,T>() will convert into std::true_type or std::false_type depending upon the value of T. Also note that std::true_type and std::false_type are nothing but just typedefs, defined as:
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
I know this question has been answered but nobody mentioned that std::enable_if can be used as a second template parameter like this:
#include <type_traits>
class A {};
class B: public A {};
template<class T, typename std::enable_if<std::is_base_of<A, T>::value, int>::type = 0>
int foo(T t)
{
return 1;
}
I like this clear style:
void foo_detail(T a, const std::true_type&)
{
//do sub-class thing
}
void foo_detail(T a, const std::false_type&)
{
//do else
}
void foo(T a)
{
foo_detail(a, std::is_base_of<Bar, T>::value);
}
The problem is that indeed you cannot do something like this in C++17:
template<T>
struct convert_t {
static auto convert(T t) { /* err: no specialization */ }
}
template<T>
struct convert_t<T> {
// T should be subject to the constraint that it's a subclass of X
}
There are, however, two options to have the compiler select the correct method based on the class hierarchy involving tag dispatching and SFINAE.
Let's start with tag dispatching. The key here is that tag chosen is a pointer type. If B inherits from A, an overload with A* is selected for a value of type B*:
#include <iostream>
#include <type_traits>
struct type_to_convert {
type_to_convert(int i) : i(i) {};
type_to_convert(const type_to_convert&) = delete;
type_to_convert(type_to_convert&&) = delete;
int i;
};
struct X {
X(int i) : i(i) {};
X(const X &) = delete;
X(X &&) = delete;
public:
int i;
};
struct Y : X {
Y(int i) : X{i + 1} {}
};
struct A {};
template<typename>
static auto convert(const type_to_convert &t, int *) {
return t.i;
}
template<typename U>
static auto convert(const type_to_convert &t, X *) {
return U{t.i}; // will instantiate either X or a subtype
}
template<typename>
static auto convert(const type_to_convert &t, A *) {
return 42;
}
template<typename T /* requested type, though not necessarily gotten */>
static auto convert(const type_to_convert &t) {
return convert<T>(t, static_cast<T*>(nullptr));
}
int main() {
std::cout << convert<int>(type_to_convert{5}) << std::endl;
std::cout << convert<X>(type_to_convert{6}).i << std::endl;
std::cout << convert<Y>(type_to_convert{6}).i << std::endl;
std::cout << convert<A>(type_to_convert{-1}) << std::endl;
return 0;
}
Another option is to use SFINAE with enable_if. The key here is that while the snippet in the beginning of the question is invalid, this specialization isn't:
template<T, typename = void>
struct convert_t {
static auto convert(T t) { /* err: no specialization */ }
}
template<T>
struct convert_t<T, void> {
}
So our specializations can keep a fully generic first parameter as long we make sure only one of them is valid at any given point. For this, we need to fashion mutually exclusive conditions. Example:
template<typename T /* requested type, though not necessarily gotten */,
typename = void>
struct convert_t {
static auto convert(const type_to_convert &t) {
static_assert(!sizeof(T), "no conversion");
}
};
template<>
struct convert_t<int> {
static auto convert(const type_to_convert &t) {
return t.i;
}
};
template<typename T>
struct convert_t<T, std::enable_if_t<std::is_base_of_v<X, T>>> {
static auto convert(const type_to_convert &t) {
return T{t.i}; // will instantiate either X or a subtype
}
};
template<typename T>
struct convert_t<T, std::enable_if_t<std::is_base_of_v<A, T>>> {
static auto convert(const type_to_convert &t) {
return 42; // will instantiate either X or a subtype
}
};
template<typename T>
auto convert(const type_to_convert& t) {
return convert_t<T>::convert(t);
}
Note: the specific example in the text of the question can be solved with constexpr, though:
template<typename T>
void foo(T a) {
if constexpr(std::is_base_of_v<Bar, T>)
// do this
else
// do something else
}
If you are allowed to use C++20 concepts, all this becomes almost trivial:
template<typename T> concept IsChildOfX = std::is_base_of<X, T>::value;
// then...
template<IsChildOfX X>
void somefunc( X& x ) {...}

C++ template specialization of constructor

I have a templated class A<T, int> and two typedefs A<string, 20> and A<string, 30>.
How do I override the constructor for A<string, 20> ? The following does not work:
template <typename T, int M> class A;
typedef A<std::string, 20> one_type;
typedef A<std::string, 30> second_type;
template <typename T, int M>
class A {
public:
A(int m) {test= (m>M);}
bool test;
};
template<>
one_type::one_type() { cerr << "One type" << endl;}
I would like the class A<std::string,20> to do something that the other class doesn't. How can I do this without changing the constructor A:A(int) ?
The only thing you cannot do is use the typedef to define the constructor. Other than that, you ought to specialize the A<string,20> constructor like this:
template<> A<string,20>::A(int){}
If you want A<string,20> to have a different constructor than the generic A, you need to specialize the whole A<string,20> class:
template<> class A<string,20> {
public:
A(const string& takethistwentytimes) { cerr << "One Type" << std::endl; }
};
Assuming your really meant for A::test to be publicly accessible, you could do something like this:
#include <iostream>
template <int M>
struct ABase
{
ABase(int n) : test_( n > M )
{}
bool const test_;
};
template <typename T, int M>
struct A : ABase<M>
{
A(int n) : ABase<M>(n)
{}
};
template <typename T>
A<T, 20>::A(int n)
: ABase<20>(n)
{ std::cerr << "One type" << std::endl; }
Kick the tires:
int main(int argc, char* argv[])
{
A<int, 20> a(19);
std::cout << "a:" << a.test_ << std::endl;
A<int, 30> b(31);
std::cout << "b:" << b.test_ << std::endl;
return 0;
}
Late but a very elegant solution:
C++ 2020 introduced Constraints and Concepts. You can now conditionally enable and disable constructors and destructors!
#include <iostream>
#include <type_traits>
template<class T>
struct constructor_specialized
{
constructor_specialized() requires(std::is_same_v<T, int>)
{
std::cout << "Specialized Constructor\n";
};
constructor_specialized()
{
std::cout << "Generic Constructor\n";
};
};
int main()
{
constructor_specialized<int> int_constructor;
constructor_specialized<float> float_constructor;
};
Run the code here.
This may be a little bit late, but if you have access to c++11 you can use SFINAE to accomplish just what you want:
template <class = typename std::enable_if<
std::is_same<A<T,M>, A<std::string, 20>>::value>::type // Can be called only on A<std::string, 20>
>
A() {
// Default constructor
}
Working example
How about :
template<typename T, int M, bool dummy = (M > 20) >
class A {
public:
A(int m){
// this is true
}
};
template<typename T, int M>
class A<T,M,false> {
public:
A(int m) {
//something else
}
};
You can't with your current approach. one_type is an alias to a particular template specialization, so it gets whatever code the template has.
If you want to add code specific to one_type, you have to declare it as a subclass of A specialization, like this:
class one_type:
public A<std::string, 20>
{
one_type(int m)
: A<str::string, 20>(m)
{
cerr << "One type" << endl;
}
};
The best solution I've been able to come up with for this situation is to use a "constructor helper function":
template <typename T, int M> class A;
typedef A<std::string, 20> one_type;
typedef A<std::string, 30> second_type;
template <typename T, int M>
class A {
private:
void cons_helper(int m) {test= (m>M);}
public:
A(int m) { cons_helper(m); }
bool test;
};
template <>
void one_type::cons_helper(int) { cerr << "One type" << endl;}