Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++0x "auto" equivalence in non-0x? (function needs to return undeterminedtype value)

Reply
Thread Tools

C++0x "auto" equivalence in non-0x? (function needs to return undeterminedtype value)

 
 
m0shbear
Guest
Posts: n/a
 
      05-06-2011
On May 6, 3:56*am, Qi <(E-Mail Removed)> wrote:
> On 2011-5-6 15:00, Kai-Uwe Bux wrote:
>
>
>
> > Alternatively, you could make A, B, C inherit from a common base and make
> > run() a virtual method. You could also use duck typing: keep A, B, and C
> > unrelated but provide a class Runnable that can accommodate values of A, B,
> > and C:

>
> I would like avoid virtual functions since the processing are quite
> cpu intensive.
>
> I would prefer compile time polymorphism over runtime virtual functions.


Boost::any isn't virtual. But you have to try-catch bad casts to infer
type.
 
Reply With Quote
 
 
 
 
Johannes Schaub
Guest
Posts: n/a
 
      05-06-2011
Qi wrote:

> What I want to do is,
>
> template <typename T>
> void process(const T & value);
>
> UndeterminedType generate();
>
> "generate" may return value in various types, which is unknown when it's
> called. What's determined is that all types are already known and
> defined.
> So I know "generate" can return a value in type A, B or C, but I don't
> know which one it exactly is.
>
> The result from "generate" is only used to feed "process", I don't need
> to store it. But it will be good if I can store it.
>
> Seems "auto" keyword in C++0x is introduced to solve that problem.
>


Looks like you are confused. "auto" has nothing to do with that. What you
want to use is a boost::variant<A, B, C> or equivalent.

 
Reply With Quote
 
 
 
 
Johannes Schaub
Guest
Posts: n/a
 
      05-06-2011
m0shbear wrote:

> On May 6, 3:56 am, Qi <(E-Mail Removed)> wrote:
>> On 2011-5-6 15:00, Kai-Uwe Bux wrote:
>>
>>
>>
>> > Alternatively, you could make A, B, C inherit from a common base and
>> > make run() a virtual method. You could also use duck typing: keep A, B,
>> > and C unrelated but provide a class Runnable that can accommodate
>> > values of A, B, and C:

>>
>> I would like avoid virtual functions since the processing are quite
>> cpu intensive.
>>
>> I would prefer compile time polymorphism over runtime virtual functions.

>
> Boost::any isn't virtual. But you have to try-catch bad casts to infer
> type.


That's plain wrong. boost::any performs type-erasure, and needs runtime
polymorphism to remember the dynamic type of the stored object. It uses
virtual functions (clone, etc..).

boost::any is inefficient, and is designed to be *simple*. It doesn't do
stack-allocation, so it will also use the heap.

 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      05-06-2011
Hi,

Qi wrote:
> On 2011-5-6 15:00, Kai-Uwe Bux wrote:
>>
>> Alternatively, you could make A, B, C inherit from a common base and make
>> run() a virtual method. You could also use duck typing: keep A, B, and C
>> unrelated but provide a class Runnable that can accommodate values of
>> A, B,
>> and C:

>
> I would like avoid virtual functions since the processing are quite
> cpu intensive.


oh, you are counting clock cycles. Hopefully you counted the right ones.

> I would prefer compile time polymorphism over runtime virtual functions.


Anyway, you will never be able to use compile time polymorphism if the
type decision is taken at run time (in the generate function).


Marcel
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-08-2011
On May 6, 3:56 am, Qi <(E-Mail Removed)> wrote:
> What I want to do is,


> template <typename T>
> void process(const T & value);


> UndeterminedType generate();


> "generate" may return value in various types, which is unknown when it's
> called. What's determined is that all types are already known and
> defined.
> So I know "generate" can return a value in type A, B or C, but I don't
> know which one it exactly is.


> The result from "generate" is only used to feed "process", I don't need
> to store it. But it will be good if I can store it.


> Seems "auto" keyword in C++0x is introduced to solve that problem.


No. "auto" is purely a compile time analysis. As are
templates. The only question is what type "generate" is
declared to return.

--
James Kanze
 
Reply With Quote
 
m0shbear
Guest
Posts: n/a
 
      05-08-2011
On May 6, 8:32*am, Johannes Schaub <(E-Mail Removed)>
wrote:
> m0shbear wrote:
> > On May 6, 3:56 am, Qi <(E-Mail Removed)> wrote:
> >> On 2011-5-6 15:00, Kai-Uwe Bux wrote:

>
> >> > Alternatively, you could make A, B, C inherit from a common base and
> >> > make run() a virtual method. You could also use duck typing: keep A,B,
> >> > and C unrelated but provide a class Runnable that can accommodate
> >> > values of A, B, and C:

>
> >> I would like avoid virtual functions since the processing are quite
> >> cpu intensive.

>
> >> I would prefer compile time polymorphism over runtime virtual functions.

>
> > Boost::any isn't virtual. But you have to try-catch bad casts to infer
> > type.

>
> That's plain wrong. boost::any performs type-erasure, and needs runtime
> polymorphism to remember the dynamic type of the stored object. It uses
> virtual functions (clone, etc..).
>
> boost::any is inefficient, and is designed to be *simple*. It doesn't do
> stack-allocation, so it will also use the heap.


My bad. I'm still somewhat of a n00b when it comes to boost. In
hindsight, I should've said boost::variant, as boost::any looks like a
dynamic_cast-checked conversion from void*.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Equivalence checking Rick Jackson VHDL 5 11-24-2005 08:38 AM
Equivalence checkers for clocks Forgafun VHDL 1 10-01-2005 03:58 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Re: Equivalence checking ALuPin VHDL 1 04-06-2004 06:56 AM
What is the equivalence of ActiveX, COM, COM+ and DCOM in .NET MS News \(MS ILM\) ASP .Net 0 08-28-2003 09:30 AM



Advertisments