Strange snippet around operator T()

I was trying to find some decent way to declare named constants in my code and ended up with the following snippet : struct const_one { typedef int type; const_one() {} template<class T> const_one(T const& ) {} template<class T> operator type() const { static type const v = {1}; return v; } template<class T> operator T() const { static T const v = {1}; return v; } }; static const const_one one_; This allow me to have the following behavior : int k = one_; float e = one_; and have proper transtyping taking place when needed. Turning this into a macro allow to declare a large sample of named constant that auto-transtype them when needed. Now funny thing is that this structure allow me to do stuff like : float k = one_ / ( 1. + three_); Now if I remove the operator type(), I can't. But if i add a printf statement in this one, it is never called. Do someone has a clue about this behavior or am I playing with some undefined behavior ? TIA -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35

AMDG Joel Falcou wrote:
I was trying to find some decent way to declare named constants in my code and ended up with the following snippet :
<snip>
Now funny thing is that this structure allow me to do stuff like : float k = one_ / ( 1. + three_);
Now if I remove the operator type(), I can't. But if i add a printf statement in this one, it is never called. Do someone has a clue about this behavior or am I playing with some undefined behavior ?
como 4.3.10.1 beta 2 accepts both. In Christ, Steven Watanabe

Steven Watanabe a écrit :
como 4.3.10.1 beta 2 accepts both.
Interesting. ICC seems to behave like gcc on this one and fail to resolve call to thing like -one_ while it obviously resolves: -int(one_) Guess I have things to do to make it work properly -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35

AMDG Joel Falcou wrote:
Steven Watanabe a écrit :
como 4.3.10.1 beta 2 accepts both.
Interesting. ICC seems to behave like gcc on this one and fail to resolve call to thing like -one_
Yeah. This is guaranteed to fail because there is no way to deduce T. In Christ, Steven Watanabe
participants (2)
-
Joel Falcou
-
Steven Watanabe