473,508 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Attempt at initialising a class with a vector "inline"

Hello all!

I am trying to write code that allows me to initialise one of my classes
inline (with a vector like structure). Inline might not be the best
term to use here but I can't think of any other right now. Here is my code:

template <typename T>
class Array {
public:
Array& operator()(const T& v) {
a.push_back(v);
return *this;
}

Array& foo(const T& v) {
return *this;
}

private:
std::vector<T> a;
};

class M {
public:
M(const Array<char>&) { }
};

int main() {
M m(Array<char>()(0)(1)(2)(1)); // XXX
return 0;
}

At the line marked with XXX you can see what I want to accomplish. My
problem is that this won't compile. However, if I call Array::foo()
instead of operator() it compiles fine. This confuses me. Maybe someone
on this list can enlighten me as to what is happening.

Regards,
Mattias
Jul 22 '05 #1
6 3124
Mattias Brändström <th*******@brasse.org> wrote in
news:41*********************@news.luth.se:
Hello all!

I am trying to write code that allows me to initialise one of my
classes
inline (with a vector like structure). Inline might not be the best
term to use here but I can't think of any other right now. Here is my
code:

template <typename T>
class Array {
public:
Array& operator()(const T& v) {
a.push_back(v);
return *this;
}

Array& foo(const T& v) {
return *this;
}

private:
std::vector<T> a;
};

class M {
public:
M(const Array<char>&) { }
};

int main() {
M m(Array<char>()(0)(1)(2)(1)); // XXX
return 0;
}

At the line marked with XXX you can see what I want to accomplish. My
problem is that this won't compile. However, if I call Array::foo()
instead of operator() it compiles fine. This confuses me. Maybe
someone on this list can enlighten me as to what is happening.


Do you really believe that "someone on this list" has a crystal ball or
something, and can magically see what error messages are you getting?

Btw. the code above compiles without any errors for me. -- Now, please
would you magically guess what compiler I'm using?

Cheers.

--
:: bartekd / o2 pl
:: "out of confusion comes chaos -- out of chaos comes confusion and fear
:: -- then comes lunch."

Jul 22 '05 #2
bartek wrote:
Do you really believe that "someone on this list" has a crystal ball or
something, and can magically see what error messages are you getting?

Btw. the code above compiles without any errors for me. -- Now, please
would you magically guess what compiler I'm using?


My bad.

The compiler I'm using is gcc 3.3.3 and the error message looks like this:

foo.cpp: In function `int main()':
foo.cpp:25: error: syntax error before numeric constant
foo.cpp:25: error: function `M m(...)' is initialized like a variable
foo.cpp:25: error: invalid declarator
foo.cpp:25: error: invalid declarator
foo.cpp:25: error: syntax error before `)' token

:.:: mattias
Jul 22 '05 #3
Mattias Brändström <th*******@brasse.org> wrote in
news:41*********************@news.luth.se:
The compiler I'm using is gcc 3.3.3 and the error message looks like
this:

foo.cpp: In function `int main()':
foo.cpp:25: error: syntax error before numeric constant
foo.cpp:25: error: function `M m(...)' is initialized like a variable
foo.cpp:25: error: invalid declarator
foo.cpp:25: error: invalid declarator
foo.cpp:25: error: syntax error before `)' token


It's most probably a gcc bug. The code gets compiled smoothly with Comeau
Online, and M$VC++ 2003.

Sorry.

--
:: bartekd / o2 pl
:: "out of confusion comes chaos -- out of chaos comes confusion and fear
:: -- then comes lunch."

Jul 22 '05 #4
Mattias Brändström wrote in news:41*********************@news.luth.se in
comp.lang.c++:
Hello all!

I am trying to write code that allows me to initialise one of my
classes
inline (with a vector like structure). Inline might not be the best
term to use here but I can't think of any other right now. Here is my
code:

#include <vector>

Please post compilable snippets when possible.
template <typename T>
class Array {
public:
Array& operator()(const T& v) {
a.push_back(v);
return *this;
}

Array& foo(const T& v) {
return *this;
}

private:
std::vector<T> a;
};

class M {
public:
M(const Array<char>&) { }
};

int main() {
M m(Array<char>()(0)(1)(2)(1)); // XXX


This is the initialization as function declaration problem, aka
"C++'s most vexing parse" fix it with:

M m = Array<char>()(0)(1)(2)(1);

Here's another example:

int i( int() );

This is parsed as:

int i( int (*)( void ) );

I.e. a declaration of a function taking function pointer
and retuning int.

Your example simplified:

M m( Array()( XXX ) );

The compiler tries to parse this as (someting like):

M m( Array (*)(*)( void )( XXX ) );

I.e. a function 'm' that returns an 'M' and takes a function
pointer to a function that returns a function-pointer (*).

When it sees XXX it expects a paramiter declaration, but gets a
literal 0, so you get the "compile time constant" error message.

*) I'm actually just Guessing here, nobody in there right mind
would intentionaly write code like this.

The point is that even though I can't make any sense of it
the compiler at least tries too :).

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
Rob Williscroft <rt*@freenet.co.uk> wrote in
news:Xn**********************************@130.133. 1.4:

This is the initialization as function declaration problem, aka
"C++'s most vexing parse" fix it with:

M m = Array<char>()(0)(1)(2)(1);

Here's another example:

int i( int() );

This is parsed as:

int i( int (*)( void ) );

I.e. a declaration of a function taking function pointer
and retuning int.

Your example simplified:

M m( Array()( XXX ) );

The compiler tries to parse this as (someting like):

M m( Array (*)(*)( void )( XXX ) );

I.e. a function 'm' that returns an 'M' and takes a function
pointer to a function that returns a function-pointer (*).

When it sees XXX it expects a paramiter declaration, but gets a
literal 0, so you get the "compile time constant" error message.

*) I'm actually just Guessing here, nobody in there right mind
would intentionaly write code like this.

The point is that even though I can't make any sense of it
the compiler at least tries too :).


At least Comeau and M$VC++ 2003 make enough sense of it to compile the
following code. Note the additional method call in main() to check what
actually got recognised is really an instantiation of the class.

Unfortunately, it seems GCC is not smart enough (yet)...

#include <vector>

template <typename T>
class Array {
public:
Array& operator()(const T& v) {
a.push_back(v);
return *this;
}
private:
std::vector<T> a;
};

class Test {
public:
Test(const Array<char>&) { }
void blah() const { }
};

int main() {
Test t( Array<char>()(0)(1)(2)(1) );
t.blah();

return 0;
}

--
:: bartekd / o2 pl
:: "out of confusion comes chaos -- out of chaos comes confusion and fear
:: -- then comes lunch."

Jul 22 '05 #6
bartek wrote in news:Xn**********************************@153.19.2 51.200 in
comp.lang.c++:

#include <vector>

template <typename T>
class Array {
public:
Array& operator()(const T& v) {
a.push_back(v);
return *this;
}
private:
std::vector<T> a;
};

class Test {
public:
Test(const Array<char>&) { }
void blah() const { }
};

int main() {
Test t( Array<char>()(0)(1)(2)(1) );
t.blah();

return 0;
}


Yep you are correct, for the record gcc 3.4 compiles fine too,
though gcc 3.2 doesn't.

Really confused as to how I got my previous results, as I was
puting the code through 4 different compilers :(.

Bizzarly gcc 3.2 doesn't handle:

M m = (Array<char>()(0)(1)(2)(1));

But does handle:

M m = Array<char>()(0)(1)(2)(1);

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2742
by: Chris Mantoulidis | last post by:
I am not clear with the use of the keyword inline... I believe you add it do a function when you implement the function inside the header file where the class is stored... But is that all? What...
9
2027
by: John Rambo | last post by:
Hi, I made the following test program: //////////////////////// classes_1.cpp #include <iostream> #include "classes_1.h" using namespace std; A::A():i(0){cout <<"const A: i =" << i <<endl;}...
10
3290
by: Christian Staudenmayer | last post by:
Hi, is there any revision of the C standard that allows the "inline" keyword (or a similar feature)? I know it is possible in gcc, but then it might be a gcc feature only. Greetings, Chris ...
14
2081
by: Frederick Gotham | last post by:
The original purpose of "inline" was that code could be "expanded in place". Of course, it has other uses... For one thing, the following two translation units will compile together succesfully...
2
8673
by: Steve Richter | last post by:
I would like to use display:inline and other CSS attributes to build an entry form. Where the heading to the left of the text box is always a set width. It is not working so I am experimenting...
12
11508
by: Dave H. | last post by:
Please redirect me if this message is posted to the wrong group. Given the intention of delivering content to an HTTP user agent (such as Internet Explorer) which is to be immediately opened by...
3
3120
by: Baron Samedi | last post by:
I am looking for a reliable cross-browser pull-quote solution in CSS. See for instance, http://www.sitepoint.com/examples/pullquotes/ The problem seems at first to be sure that it functions...
17
8352
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.