{} initializer as expression

  • Thread starter Pierre Barbier de Reuille
  • Start date
P

Pierre Barbier de Reuille

Hi,

I was wondering if this kind of things are valid as for the C++
standard? It is working with g++, but it may be either a bug (?) or an
"extension" ...

#include <iostream>

template <size_t N>
struct idx
{
typedef const size_t id[ N ];
void set( id i )
{
size_t j;
for( j = 0 ; j < N ; ++j )
{
i_[ j ] = i[ j ];
}
}

size_t i_[ N ];
};

int main()
{
typedef idx<4>::id id;
idx<4> i;
i.set( (id) {2,3,4,5} ); // Initializer used as expression
return 0;
}

Thanks,

Pierre
 
V

Victor Bazarov

Pierre said:
I was wondering if this kind of things are valid as for the C++
standard? It is working with g++, but it may be either a bug (?) or an
"extension" ...

It's an extension. In the standard C++ there are no "compound literals"
(or whatever you prefer to name those things).

V
 
C

Clark S. Cox III

Hi,

I was wondering if this kind of things are valid as for the C++
standard? It is working with g++, but it may be either a bug (?) or an
"extension" ...

#include <iostream>

template <size_t N>
struct idx
{
typedef const size_t id[ N ];
void set( id i )
{
size_t j;
for( j = 0 ; j < N ; ++j )
{
i_[ j ] = i[ j ];
}
}

size_t i_[ N ];
};

int main()
{
typedef idx<4>::id id;
idx<4> i;
i.set( (id) {2,3,4,5} ); // Initializer used as expression
return 0;
}

It's an extension. GCC allows some C99-isms in C++ code as an extension.
http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Compound-Literals.html
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top