Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible compiler bug for c4930

I just want to post this here to make sure I'm not missing something completely obvious (and will hence feel very silly). This is a simplified version of some code I wrote.

template <int NDIM, typename T = int>
class COORD {
public:
    COORD(T dim0) { m_data[0] = dim0; }
private:
    T m_data[NDIM];
};

template <class COORD>
class NODE {
public:
    NODE(const COORD& c = COORD()) : m_coord(c) {}
private:
COORD m_coord;
};

int main()
{
    const int VAL = 10;
    NODE< COORD<1> > n( COORD<1>( VAL ) ); // warning c4930???
    NODE< COORD<1> > n2( COORD<1>( 10 ) );
    COORD<1> c( VAL );
    NODE< COORD<1> > n3( c );
    return 0;
}

The statement "NODE< COORD<1> > n( COORD<1>( VAL ) );" gives:

warning C4930: 'NODE<COORD> n(COORD<NDIM>)': prototyped function not called
(was a variable definition intended?)
    with
    [
        COORD=COORD<1>,
        NDIM=1
    ]

But, the decls for 'n2' and 'n3' compile fine. This was with VC2008 & VC2010, I haven't tried VC2012 yet. This block of code seems to compile fine on GCC 4.5.3.

like image 328
MarkB Avatar asked Dec 21 '22 17:12

MarkB


1 Answers

That's known as the "most vexing parse". You may think you're creating a temporary object and using it to initialise a variable; but the compiler interprets it as a function declaration. Specifically, n is a function returning NODE<COORD<1>>, taking an argument which is a pointer to a function taking an parameter called VAL (with a missing type specifier), and returning COORD<1>.

You can fix it by adding parentheses so it isn't interpreted as a function declaration:

NODE< COORD<1> > n( ( COORD<1>( VAL ) ) );
                    ^                 ^
like image 97
Mike Seymour Avatar answered Dec 24 '22 00:12

Mike Seymour