[cctbxbb] Idiot C++ question

Phil Evans pre at mrc-lmb.cam.ac.uk
Tue May 15 23:42:21 PDT 2012


Fair enough

My naive question arose from surprise I suppose. The constructor syntax seems natural, and as a non-C programmer the C-cast syntax "(double)i" looks weird

thanks
Phil

On 16 May 2012, at 07:01, Ralf Grosse-Kunstleve wrote:

> Hi Phil,
> The main reasons for me to prefer static_cast is clarity; I just want to make the type conversion easily noticeable.
> Ralf
> 
> On Tue, May 15, 2012 at 3:59 AM, Phil Evans <pre at mrc-lmb.cam.ac.uk> wrote:
> 
> I can see that problem with pointers but it doesn't seem to apply to double
> 
> I am naive about this, I suppose
> 
> 1) I never learnt C, just C++
> 2) I don't use pointers (or very rarely)
> 3) I read "double(i)" as a constructor of a double from an int ie
> 
> class double {
> public:
>        double(const int& i);
> };
> 
> thus it shouldn't accept eg
> 
> thing* instance;
> d = double(instance);
> 
> which should be a compiler error, just as an illegal construction of any other object should be
> 
> and indeed
>  double* pd;
>  double d = double(pd);
> 
> gives
> t.cpp:4:23: error: invalid cast from type 'double*' to type 'double'
> 
> Phil
> 
> 
> On 15 May 2012, at 11:38, Luc Bourhis wrote:
> 
> > Hi Phil,
> >
> >> Looking at some cctbx C++ code, I note use of syntax like
> >>
> >> int i = 1234;
> >> double d = static_cast<double>(i);
> >>
> >> How is this different or indeed preferable to
> >>
> >> double d = double(i);
> >>
> >> or
> >>
> >> d = i;
> >>
> >> ?
> >
> >
> > Short answer: For conversions between non-pointer types, it does not matter but for pointer types, static_cast shall always be used to the exclusion of any other conversion.
> >
> > The language lawyer:
> >
> > The construct
> >
> > double d = double(i)
> >
> > which is known as functional-style cast is completely equivalent to the so-call cast notation, which comes from C:
> >
> > double d = (double)i
> >
> > You may see both in a few places in the cctbx code actually.
> >
> > The problem with those two constructs is that the compiler will let use convert a pointer to anything into a pointer to anything else. Consider:
> >
> > #include <iostream>
> >
> > struct foo {
> >  int i,j;
> >  foo(int i, int j): i(i), j(j) {}
> > };
> >
> > struct bar {
> >  double d;
> >  bar(double d): d(d) {}
> > };
> >
> > int main() {
> >  foo x(1, 2);
> >  bar *y = (bar *)&x;
> >  std::cout << y->d << std::endl;
> >  return 0;
> > }
> >
> > This prints 4.24399e-314 on my computer but this depends on the compiler. On the contrary,
> >
> > bar *y = static_cast<bar *>(&x)
> >
> > is not legit and the compiler will bug you.
> >
> > HtH,
> >
> > Luc
> >
> >
> >
> >
> > _______________________________________________
> > cctbxbb mailing list
> > cctbxbb at phenix-online.org
> > http://phenix-online.org/mailman/listinfo/cctbxbb
> 
> _______________________________________________
> cctbxbb mailing list
> cctbxbb at phenix-online.org
> http://phenix-online.org/mailman/listinfo/cctbxbb
> 
> _______________________________________________
> cctbxbb mailing list
> cctbxbb at phenix-online.org
> http://phenix-online.org/mailman/listinfo/cctbxbb



More information about the cctbxbb mailing list