How to walk around this compilation error?

K

kuangye

How to walk around this compilation error?

////////////////////////////
template<bool flag>
class COMPILER_STATIC_CHECK{
};

template<>
class COMPILER_STATIC_CHECK<true>{
public:
enum{OK=1,};
public:
COMPILER_STATIC_CHECK(...){}
};


#define STATIC_CHECK_BY_EVAL(CheckName, cnstExprVal)\
enum {CheckName= COMPILER_STATIC_CHECK<((bool)(cnstExprVal))


enum STID
{
I32ID=1,
F32ID=20,
};

template<STID tid>
class Scalar;

template<typename T>
class Complex;


template<STID tid>
class Scalar
{
public:
typedef Scalar SelfType;
static const STID tid = tid;

SelfType operator+(const SelfType&)
{
return *this;
}

Complex<SelfType> operator+(const Complex<SelfType>& src);
};

template<typename T>
class Complex
{
//////////////////////////////check
////I need this check to forbidden programmer to instantiate
Complex<I32>
////
STATIC_CHECK_BY_EVAL(TShouldBeF, T::tid==20);
///////////////////////////
public:
typedef Complex SelfType;
};

template<STID tid>
Complex< Scalar<tid> > Scalar<tid>::eek:perator+(const Complex<SelfType>&
src2)
{
return src2;
}

typedef Scalar<I32ID> I32;
typedef Scalar<F32ID> F32;

int main()
{
I32 i1, i2, i3;
i1 + i2;
return 0;
}

compilation error message:
=========================
error C2039: 'OK' : is not a member of 'COMPILER_STATIC_CHECK<flag>'
[
flag=false
]
see reference to class template instantiation 'Complex<T>' being
compiled
with
[
T=S
]
=========================

Seems that finding the candidate functions will cause class template
instantiation.
How to bypass this compilation error
 
V

Vaclav Haisman

kuangye wrote, On 6.8.2009 6:26:
How to walk around this compilation error?

////////////////////////////
template<bool flag>
class COMPILER_STATIC_CHECK{
};

template<>
class COMPILER_STATIC_CHECK<true>{
public:
enum{OK=1,};
public:
COMPILER_STATIC_CHECK(...){}
};


#define STATIC_CHECK_BY_EVAL(CheckName, cnstExprVal)\
enum {CheckName= COMPILER_STATIC_CHECK<((bool)(cnstExprVal))


enum STID
{
I32ID=1,
F32ID=20,
};

template<STID tid>
class Scalar;

template<typename T>
class Complex;


template<STID tid>
class Scalar
{
public:
typedef Scalar SelfType;
static const STID tid = tid;

SelfType operator+(const SelfType&)
{
return *this;
}

Complex<SelfType> operator+(const Complex<SelfType>& src);
};

template<typename T>
class Complex
{
//////////////////////////////check
////I need this check to forbidden programmer to instantiate
Complex<I32>
////
STATIC_CHECK_BY_EVAL(TShouldBeF, T::tid==20);
///////////////////////////
public:
typedef Complex SelfType;
};

template<STID tid>
Complex< Scalar<tid> > Scalar<tid>::eek:perator+(const Complex<SelfType>&
src2)
{
return src2;
}

typedef Scalar<I32ID> I32;
typedef Scalar<F32ID> F32;

int main()
{
I32 i1, i2, i3;
i1 + i2;
return 0;
}

compilation error message:
=========================
error C2039: 'OK' : is not a member of 'COMPILER_STATIC_CHECK<flag>'
[
flag=false
]
see reference to class template instantiation 'Complex<T>' being
compiled
with
[
T=S
]
=========================

Seems that finding the candidate functions will cause class template
instantiation.
How to bypass this compilation error
Your question does not make much sense. The code is doing what it is designed
to do, it is preventing instantiation of Complex<I32> because its ID is I32ID
which has value 1, not 20.
 
K

kuangye

Thanks, Vaclav Haisman

When using "i1 + i2"; I expect "I32 operator+(const I32& src)" to be
called.
Actually, it will be if i remove "STATIC_CHECK_BY_EVAL(TShouldBeF,
T::tid==20); " in Complex definition.

However, the the template overload resolution try to deduce the
Sclar::eek:perator+(const Complex&) and add to the candidate functions
list. This behavior seems to cause the template class instantiation
which trigger the static check.

But we know "i1 + i2" will not call Scalar::eek:perator+(const Complex&),
Why not ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top