From: red floyd on
ManicQin wrote:
> Hi,
> It would be nice if somebody would be able to give be a pointer here.
> I have a function with a templated return type.
>
> for example:
> template <class T>
> T foo()
> {
> T retVal;
>
> //Just for an example
> retVal += 10;
>
> return retVal;
> }
[redacted]

>
> Is there a way to initialize the val?
> Is there a different way to work in order to overcome this problem?
>

T retval = T();

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: CornedBee on
On Nov 29, 8:06 pm, ManicQin <manic...(a)gmail.com> wrote:
> I cannot predict what type will be used, it could be a P.O.D. or any
> class that implements +=, and I cant write
> T retval(0); in order to initialize the variable.
>
> Is there a way to initialize the val?

T retval = T();

Sebastian


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
Johannes Schaub (litb) wrote:

> ManicQin wrote:
>
>> Hi,
>> It would be nice if somebody would be able to give be a pointer here.
>> I have a function with a templated return type.
>>
>> for example:
>> template <class T>
>> T foo()
>> {
>> T retVal;
>>
>> //Just for an example
>> retVal += 10;
>>
>> return retVal;
>> }
>>
>> Let's not dwell on the purpose of the function please,
>> Basically we can use ... almost all types that "allow" the operator +=
>> When running the code the compiler will scream (and rightfully)
>> Run-Time Check Failure #3 - The variable 'retVal' is being used
>> without being initialized.
>>
>> I cannot predict what type will be used, it could be a P.O.D. or any
>> class that implements +=, and I cant write
>> T retval(0); in order to initialize the variable.
>>
>> Is there a way to initialize the val?
>> Is there a different way to work in order to overcome this problem?
>>
>
> Quick one, that fails if the type is non-copyable. But this doesn't
> matter, as you need to copy "T" anyway if you wanna return by value:
>
> T retVal((T()));
>
> Fixing it involves some workaround to overcome the impossible "T
> retVal();" object declaration syntax.
>
> struct { T t; } u = {};
>

Haha, the irony. Should have tested that "trick". Shortly afterwards i was
reminded of this bug report to GCC
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40239 . In fact the Standard
requires a copy constructor for T also here when read extremely narrowly.

I guess you need to write a bit more now if you don't want to imply the copy
constructible requirement. Like boost::value_initialized<>:

struct u_ { u_():t() {} T t; } u;

That will now correctly not require a copy constructor. Have fun, and sorry
for my double post :)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Kenshin on
On Nov 29, 10:06 pm, ManicQin <manic...(a)gmail.com> wrote:
> Hi,
> It would be nice if somebody would be able to give be a pointer here.
> I have a function with a templated return type.
>
> for example:
> template <class T>
> T foo()
> {
> T retVal;
>
> //Just for an example
> retVal += 10;
>
> return retVal;
>
> }
>
> Let's not dwell on the purpose of the function please,
> Basically we can use ... almost all types that "allow" the operator +=
> When running the code the compiler will scream (and rightfully)
> Run-Time Check Failure #3 - The variable 'retVal' is being used
> without being initialized.
>
> I cannot predict what type will be used, it could be a P.O.D. or any
> class that implements +=, and I cant write
> T retval(0); in order to initialize the variable.
>
> Is there a way to initialize the val?
> Is there a different way to work in order to overcome this problem?
>
> thanks
>

Do you get this error at point-of-definition, or at point-of-call? If
at point of call, make sure you call it like:

foo<Data-Type>();

Where Data_Type is self-explanatory.
In addition CornedBee below suggests, T retval = T(), which the std
libs also uses. Try that.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: ManicQin on
On Nov 30, 6:11 am, red floyd <no.spam.h...(a)its.invalid> wrote:

>
> T retval = T();
>

Thank you all,

The thing is that I have a function that executes a "command" class on
an unknown set of arguments
and accumulates the results in a templated fashion...

//RetMod is the method of returning the values, I can AND\XOR them
like I do with my booleans
//and I can concat them like I do with my strings (and some other
operations I do with my objetcs
//The CALL_MEMBER_FN is a macro of mine to help me extract the command
from the queue and inVal is off course the val to be used.
//RetMod()(retVal,CALL_MEMBER_FN(*(*iter),Functor)(inVal1));

I think I'll adopt Daniel Kr�gler's trait,
A silly question:
Is there any reason for me to prefer a copy constructor or assignment?

// T retVal(ZeroTraits<T>::zero());
// T retVal = ZeroTraits<T>::zero();

Thanks!


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]