Boost logo

Boost Users :

Subject: Re: [Boost-users] BOOST_STRONG_TYPEDEF
From: Agustín K-ballo Bergé (kaballo86_at_[hidden])
Date: 2014-08-07 21:11:40


On 07/08/2014 09:40 p.m., Jono Poff wrote:
> Hi,
>
> compiling this program with g++ 4.7.1 ...
>
>
> #include <boost/serialization/strong_typedef.hpp>
> BOOST_STRONG_TYPEDEF(uint32_t, LINKID_T)
>
> int main()
> {
> LINKID_T lid = 0xffddee33;
> return 0;
> }
>
> I get
>
>
> error: conversion from 'unsigned int' to non-scalar type
> 'LINKID_T' requested
>
>
> Looking at the header, the strong typedef struct defines assignment from
> uint32_t to LINKID_T ...
>
>
> D & operator=(const T & rhs) { t = rhs; return *this;}
>
>
> ... so what gives?

The expression `LINKID_T lid = 0xffddee33;` would be doing construction,
not assignment. The constructor that would be picked would be the
conversion construction `explicit D(const T t_) : t(t_) {};` followed by
the copy construction `D(const D & t_) : t(t_.t){}`, but the conversion
constructor is not viable because it is `explicit`.

What you can do is either:

     LINKID_T lid = LINKID_T(0xffddee33);

or simply:

     LINKID_T lid(0xffddee33);

Regards,

-- 
Agustín K-ballo Bergé.-
http://talesofcpp.fusionfenix.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net