Re: g++ doesn't find the right ctor

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Matthias Kretz <Matthias.Kretz@xxxxxxxxxxxxxxxxxxxxx> writes:

> I believe g++ is at fault here.

You get a clearer error if you get rid of the template:

struct B
{
    float *f;
};

struct A
{
    A(const B *, const float *B::*) {}
};

int main()
{
    B x;
    A y(&x, &B::f);
    return 0;
}

g++-4.3.1 reports:
constructor.cc: In function ‘int main()’:
constructor.cc:14: error: invalid conversion from ‘float* B::*’ to ‘const float* B::*’
constructor.cc:14: error:   initializing argument 2 of ‘A::A(const B*, const float* B::*)’

There is no implicit conversion from float* B::* to const float* B::*.
There is however a conversion to const float* const B::*.
So if you change the original template to

struct A
{
    template<typename T> A(const T *, const float *const T::*) {}
};

then it will compile.

If there were an implicit connversion from float* B::* to
const float* B::*, you could inadvertently do:

struct B
{
    float *f;
};

const float c = 0;

int main()
{
    B b;
    float* B::*p = &B::f;
    const float* B::*q = p; /* luckily not allowed */
    b.*q = &c; /* modifies b.f */
    *b.f = 1; /* tries to modify c, raises SIGSEGV */
}

which perhaps is less likely than inadvertently dereferencing a
pointer that points to freed memory, but still.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux