yigal_s: (Default)
yigal_s ([personal profile] yigal_s) wrote2011-09-30 01:31 am
Entry tags:

C++ этюд. Функция, принимающая точный тип int. (преобразования типа запрещены)

template< typename T, typename TT >
class ErrorGenerator2
{
// const int errorGen=0; - this is underfined! :-)
};


template < typename T>
class ExactType
{
T mT;
public:
ExactType(T t)
: mT(t)
{}

template < typename TT >
ExactType(TT tt)
{
ErrorGenerator2< T, TT>::errorGen; // error generator: 2nd type is disallowed, use 1st only
}
T get()
{ return mT; }
};

void funcExact(ExactType< int > arg)
{

}
enum ESomeEnum { aaa};

void test()
{
funcExact(aaa);
short si=1;
funcExact(si);
}

Таким трюком можно, например, защищать конструкторы классов, принимающие один целый аргумент.
Решение не очень практичное, поскольку имеет ряд недостатков.

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting