473,661 Members | 2,440 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()(cons t 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 3136
Mattias Brändström <th*******@bras se.org> wrote in
news:41******** *************@n ews.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()(cons t 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*******@bras se.org> wrote in
news:41******** *************@n ews.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******** *************@n ews.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()(cons t 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()(cons t 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.251.200 in
comp.lang.c++:

#include <vector>

template <typename T>
class Array {
public:
Array& operator()(cons t 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
2772
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 am I missing? If that's all, then why did Bjarne even bother adding it to the language? If that's not all, what else can I do with "inline"?
9
2055
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;} A::~A(){cout <<"destr A"<<endl;} inline void A::showA() {cout << "show A: i =" << i <<endl;}; //////////////////////// classes_1.h
10
3304
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 -- Christian Staudenmayer
14
2103
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 into a program: /* source1.cpp */ inline int Func(double arg) {
2
8681
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 with two divs to get them to render side by side: <div style="width:12em;border: 2px solid #EFCE8C; padding: 0.5em;display:inline;">abc</div> <div style="width:12em;border: 2px solid #EFCE8C; padding:
12
11534
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 an associated application, there are the Content-Disposition type options of "inline" or "extension-token". Specifically as regards Internet Explorer, I've tried both inline and the specific filename extension (xls,csv,pdf,doc,...) and the...
3
3142
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 across all browsers, but there seem to be a few very similar solutions, so that is probably not a major hurdle. However, just to complicate things, I want to have it "inline" (http:// www.tizag.com/cssT/inline.php)
17
8368
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 the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1743
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.