Boost logo

Boost :

Subject: [boost] Strange snippet around operator T()
From: Joel Falcou (joel.falcou_at_[hidden])
Date: 2008-11-24 16:42:29


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

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk