POV-Ray : Newsgroups : povray.off-topic : Adventures with C++ Server Time
29 Jul 2024 02:23:51 EDT (-0400)
  Adventures with C++ (Message 31 to 40 of 65)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Kevin Wampler
Subject: Re: An actual C++ question
Date: 21 May 2013 13:32:36
Message: <519bafb4$1@news.povray.org>
On 5/21/2013 10:25 AM, Orchid Win7 v1 wrote:
> However, I can't seem to figure out what to write in the constructor.
> I've got a field that looks like
>
>    boost::shared_ptr<std::vector<Foo>> _foo;
>
> What do I need to do to initialise this correctly?


boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>());

Take a look over the constructors of shared_ptr for more detail.  I 
should note that it's relatively uncommon that I want a shared_ptr to a 
std::vector.  Unless you actually need to store _foo somewhere else in 
the code it's might be better to just forgo the shared_ptr and pass _foo 
by reference to whichever functions need it.


Post a reply to this message

From: Kevin Wampler
Subject: Re: An actual C++ question
Date: 21 May 2013 13:38:00
Message: <519bb0f8@news.povray.org>
On 5/21/2013 10:32 AM, Kevin Wampler wrote:
> On 5/21/2013 10:25 AM, Orchid Win7 v1 wrote:
>> However, I can't seem to figure out what to write in the constructor.
>> I've got a field that looks like
>>
>>    boost::shared_ptr<std::vector<Foo>> _foo;
>>
>> What do I need to do to initialise this correctly?
>
>
> boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>());
>

Oh, and it's probably best to write it like this:

boost::shared_ptr<std::vector<Foo> > _foo(new std::vector<Foo>());

Since some compilers will complain about using >> in a type definition 
(due to the ambiguity with the >> operator).


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: An actual C++ question
Date: 21 May 2013 17:07:09
Message: <519be1fd$1@news.povray.org>
On 21/05/2013 06:32 PM, Kevin Wampler wrote:
> On 5/21/2013 10:25 AM, Orchid Win7 v1 wrote:
>> However, I can't seem to figure out what to write in the constructor.
>> I've got a field that looks like
>>
>> boost::shared_ptr<std::vector<Foo>> _foo;
>>
>> What do I need to do to initialise this correctly?
>
>
> boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>());
>
> Take a look over the constructors of shared_ptr for more detail. I
> should note that it's relatively uncommon that I want a shared_ptr to a
> std::vector. Unless you actually need to store _foo somewhere else in
> the code it's might be better to just forgo the shared_ptr and pass _foo
> by reference to whichever functions need it.

The only reason I'm doing this is to avoid putting the real definition 
of Foo in the header file.


Post a reply to this message

From: Warp
Subject: Re: An actual C++ question
Date: 22 May 2013 15:45:58
Message: <519d2076@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> However, I can't seem to figure out what to write in the constructor. 
> I've got a field that looks like

>    boost::shared_ptr<std::vector<Foo>> _foo;

> What do I need to do to initialise this correctly?

What do you mean by "field"? Your question is not clear enough.

(And btw, don't start names with an underscore. Those are reserved for
the compiler.)

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: An actual C++ question
Date: 23 May 2013 03:21:03
Message: <519dc35f$1@news.povray.org>
On 22/05/2013 08:45 PM, Warp wrote:
> Orchid Win7 v1<voi### [at] devnull>  wrote:
>> However, I can't seem to figure out what to write in the constructor.
>> I've got a field that looks like
>
>>     boost::shared_ptr<std::vector<Foo>>  _foo;
>
>> What do I need to do to initialise this correctly?
>
> What do you mean by "field"? Your question is not clear enough.

I mean something like

   class Foobar
   {
     private:
       boost::shared_ptr<std::vector<Foo>> foo;
     public:
       Foobar()
   }

> (And btw, don't start names with an underscore. Those are reserved for
> the compiler.)

Hoooookay... In that case, there's about 200 lines in the existing 
codebase that need to be changed. o_O


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: An actual C++ question
Date: 23 May 2013 03:22:34
Message: <519dc3ba$1@news.povray.org>
>> boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>());
>
> Oh, and it's probably best to write it like this:
>
> boost::shared_ptr<std::vector<Foo> > _foo(new std::vector<Foo>());
>
> Since some compilers will complain about using >> in a type definition
> (due to the ambiguity with the >> operator).

VisualStudio compiles this perfectly. GCC point-blank fails to compile 
it. *sigh* Gotta love the number of inconsistencies between two 
compilers for supposedly "the same" language...

(VC emits endless warnings about "printf may be unsafe; please use 
printf_s instead". Apparently the latter function doesn't exist in GCC...)


Post a reply to this message

From: Francois Labreque
Subject: Re: An actual C++ question
Date: 23 May 2013 09:26:48
Message: <519e1918@news.povray.org>

>>> boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>());
>>
>> Oh, and it's probably best to write it like this:
>>
>> boost::shared_ptr<std::vector<Foo> > _foo(new std::vector<Foo>());
>>
>> Since some compilers will complain about using >> in a type definition
>> (due to the ambiguity with the >> operator).
>
> VisualStudio compiles this perfectly. GCC point-blank fails to compile
> it. *sigh* Gotta love the number of inconsistencies between two
> compilers for supposedly "the same" language...

You are surprised that a Microsoft product allows something that is 
frowned upon by a more anal-retentive competitor?!?

>
> (VC emits endless warnings about "printf may be unsafe; please use
> printf_s instead". Apparently the latter function doesn't exist in GCC...)

They are MS-specific versions of the functions.

http://msdn.microsoft.com/en-us/library/8ef0s5kh%28v=vs.80%29.aspx
http://msdn.microsoft.com/en-us/library/239ffwa0%28v=vs.80%29.aspx

I'm sure the purists would frown that Microsoft decides to mess with 
<sdtio.h> instead of putting them in a "msstdio.h" or similar.  Way back 
when the dinosaurs still roamed the Earth, I used to hang in comp.lang.c 
and I vividly remember certain members being quite angry at Borland for 
pretending that conio.h was part of the standard headers and its 
functions part of the standard libraries.



-- 
/*Francois Labreque*/#local a=x+y;#local b=x+a;#local c=a+b;#macro P(F//
/*    flabreque    */L)polygon{5,F,F+z,L+z,L,F pigment{rgb 9}}#end union
/*        @        */{P(0,a)P(a,b)P(b,c)P(2*a,2*b)P(2*b,b+c)P(b+c,<2,3>)
/*   gmail.com     */}camera{orthographic location<6,1.25,-6>look_at a }


Post a reply to this message

From: Kevin Wampler
Subject: Re: An actual C++ question
Date: 23 May 2013 10:11:01
Message: <519e2375$1@news.povray.org>
On 5/23/2013 6:27 AM, Francois Labreque wrote:

>>>> boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>());
>>>
>>> Oh, and it's probably best to write it like this:
>>>
>>> boost::shared_ptr<std::vector<Foo> > _foo(new std::vector<Foo>());
>>>
>>> Since some compilers will complain about using >> in a type definition
>>> (due to the ambiguity with the >> operator).
>>
>> VisualStudio compiles this perfectly. GCC point-blank fails to compile
>> it. *sigh* Gotta love the number of inconsistencies between two
>> compilers for supposedly "the same" language...
>
> You are surprised that a Microsoft product allows something that is
> frowned upon by a more anal-retentive competitor?!?
>

Actually, I'm a little surprised that GCC didn't accept it, as that sort 
of things should be allowed under the C++11 standard 
(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1757.html) and 
GCC 4.7 is listed as supporting that feature 
(http://gcc.gnu.org/gcc-4.7/cxx0x_status.html).  In general GCC seems to 
have much better C++11 support that MSVC, so it's strange to see an 
example of the opposite -- perhaps Andrew's using an old version of GCC 
or something.


Post a reply to this message

From: scott
Subject: Re: An actual C++ question
Date: 23 May 2013 10:14:06
Message: <519e242e$1@news.povray.org>
>>> boost::shared_ptr<std::vector<Foo> > _foo(new std::vector<Foo>());
>>>
>>> Since some compilers will complain about using >> in a type definition
>>> (due to the ambiguity with the >> operator).
>>
>> VisualStudio compiles this perfectly. GCC point-blank fails to compile
>> it. *sigh* Gotta love the number of inconsistencies between two
>> compilers for supposedly "the same" language...
>
> You are surprised that a Microsoft product allows something that is
> frowned upon by a more anal-retentive competitor?!?

boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>());

I don't see how the >> is ambiguous at all in this statement. Seems to 
me like lazy/bad parser design if this trips it up.


Post a reply to this message

From: Kevin Wampler
Subject: Re: An actual C++ question
Date: 23 May 2013 10:44:39
Message: <519e2b57$1@news.povray.org>
On 5/23/2013 7:14 AM, scott wrote:
>
> boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>());
>
> I don't see how the >> is ambiguous at all in this statement. Seems to
> me like lazy/bad parser design if this trips it up.
>

It's not quite so cut and dry as you might guess.  For example:


#include <iostream>
using std::cout;
using std::endl;

int _foo(int i) {
	return i;
}

namespace std {
	static const int vector = 7;
}

namespace boost {
	static const int shared_ptr = 5;
}

int main(int argc, char** argv) {
	int Foo = 3;
	cout << (boost::shared_ptr<std::vector<Foo>> _foo(NULL)) << endl;
	return 0;
}


If you run this program it should print "1" (at least in MSVC), which is 
the result of treating < and >> and the less-than and bit-shift 
operators.  Of course you couldn't write "new std::vector<Foo>()" in 
this scheme, but it does show that it's hard to tell what's a template 
and what's not at a purely syntatic level.

In any case, the C++11 standard does allow 
"boost::shared_ptr<std::vector<Foo>> _foo(new std::vector<Foo>()); " and 
the most recent version of GCC supposedly supports this.  You can read a 
bit more about it here: 
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1757.html


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.