Is this valid C++ ?

A

awk

Hi,

is this valid C++ ? It compiles fine with msvc8 and intel 10, but not
with gcc 4.1 and 4.2.

template <int N>
struct foo
{
int c[N];
};

template <int N>
struct bar : foo<N>
{
bar(int x)
{
c[0] = x;
}
};

int main(int argc, const char* argv[])
{
bar<2> tmp(3);
return 0;
}

The error is:
In constructor 'bar<N>::bar(int)':
error: 'c' was not declared in this scope

regards,
Anders
 
R

red floyd

Hi,

is this valid C++ ? It compiles fine with msvc8 and intel 10, but not
with gcc 4.1 and 4.2.

template <int N>
struct foo
{
int c[N];
};

template <int N>
struct bar : foo<N>
{
bar(int x)
{
c[0] = x;
}
};

int main(int argc, const char* argv[])
{
bar<2> tmp(3);
return 0;
}

The error is:
In constructor 'bar<N>::bar(int)':
error: 'c' was not declared in this scope

I believe gcc is correct. Use "this->c[0] = x;"
 
J

John Harrison

The error is:
In constructor 'bar<N>::bar(int)':
error: 'c' was not declared in this scope

I believe gcc is correct. Use "this->c[0] = x;"

Or 'foo<N>::c[0] = x;' and google for 'two-phase lookup' if you want an
explanation.

john
 
B

Bo Persson

(e-mail address removed) wrote:
:: Hi,
::
:: is this valid C++ ? It compiles fine with msvc8 and intel 10, but
:: not with gcc 4.1 and 4.2.
::
:: template <int N>
:: struct foo
:: {
:: int c[N];
:: };
::
:: template <int N>
:: struct bar : foo<N>
:: {
:: bar(int x)
:: {
:: c[0] = x;
:: }
:: };
::
:: int main(int argc, const char* argv[])
:: {
:: bar<2> tmp(3);
:: return 0;
:: }
::
:: The error is:
:: In constructor 'bar<N>::bar(int)':
:: error: 'c' was not declared in this scope
::
:: regards,
:: Anders


The code is not correct, as others have pointed out.

MSVC compiles this *as an extension* with default settings. If you set
the Disable Language Extensions option (/Za), it will not compile.

The Intel compiler on purpose behaves just like MSVC in compatibility
mode. Bug for bug. :)


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top