By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

Is this valid C++ ?

P: n/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<2tmp(3);
return 0;
}

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

regards,
Anders

Jun 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
aw*@imm.dtu.dk 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<2tmp(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;"
Jun 15 '07 #2

P: n/a
>>
>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
Jun 15 '07 #3

P: n/a
jg
On Jun 15, 2:03 pm, red floyd <no.s...@here.dudewrote:
>
I believe gcc is correct. Use "this->c[0] = x;"
Does "c[0] = x" means "this->c[0] = x " ?

JG

Jun 15 '07 #4

P: n/a
jg wrote:
On Jun 15, 2:03 pm, red floyd <no.s...@here.dudewrote:
>I believe gcc is correct. Use "this->c[0] = x;"

Does "c[0] = x" means "this->c[0] = x " ?

JG
Without the "this->", "c" is a nondependent name. foo<Nis a dependent
name (it depends on template parameter N). Dependent base classes are
not searched when looking up nondependent names.

This is all in the FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-35.18

--
Alan Johnson
Jun 15 '07 #5

P: n/a
aw*@imm.dtu.dk 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<2tmp(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
Jun 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.