Leandro Melo
11/27/2008 5:27:00 PM
On 27 nov, 11:03, Tim Martin <t...@promessage.com> wrote:
> I have the following code:
>
> #include <boost/variant.hpp>
>
> #include <string>
>
> using namespace std;
>
> class foo
> {
> public:
> int fish;
> string monkey;
>
> };
>
> typedef boost::variant<int foo:: *, string foo:: *> my_variant;
>
> int main(void)
> {
> const my_variant bar = &foo::fish; // (1)
>
> }
>
> It compiles fine on gcc 3.4.6 on linux, but on 3.4.3 on Solaris it fails
> to compile with an error about casting away const somewhere in the
> boost::variant library. If I change the line marked (1) to:
>
> const my_variant bar(&foo::fish);
>
> it compiles fine on both platforms. I assume that this is something to
> do with the difference between copy-initialization and
> direct-initialization, and I want to check that my understanding is correct.
>
> The way I understand it, in the copy-initialization case the compiler
> will construct a temporary my_variant by using the constructor with
> argument &foo::fish, and then copy-construct the const object bar from
> this temporary. I don't see why this should cause a constness problem. I
> should be able to copy-construct a const my_variant from a (presumably)
> non-const temporary my_variant. I can imagine that the difference
> between the platforms is due to eliding the copy on one platform, but I
> can't understand why the non-eliding platform is causing an error.
>
> Is there something C++-related I'm missing here? It could be an issue in
> the boost library implementation, in which case I'll take this elsewhere.
My guess is that it's a compiler bug. In fact, with optimization on
(even off?) these two lines will probably generate the same exact
machine code.
--
Leandro T. C. Melo