From: ManicQin on
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

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

From: Goran on
On Nov 29, 8: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?

What, you mean, beside intializing it?

No.

You say "let's not dwell on the purpose..." You're right, no need for
that, there __is__ no reasonable purpose to the example.

How can it be useful to use uninitialized variable? What are you
trying to achieve? Return supposedly random number incremented by 10?
Both your compiler (I guess, through a warning), and runtime ("Run-
time check failure") actually spotted a bug for you. Why not kindly
fix the bug (bug being the use of an uninitialized variable)?

Note also that if T is a type with a default constructor, there is no
problem. You and you alone made one, by refusing to initialize, but
using, a variable of an intrinsic type.

Goran.


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

From: Daniel Krügler on
On 29 Nov., 20:06, 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?

I suggest to introduce a traits class that equalizes
all the differences. This has the advantage that even
class types with a default c'tor that corresponds to
a NaN state could be adapted, e.g. start with

template<typename T>
struct ZeroTraits {
static T zero() { return T(); }
};

and specialize that only, if needed. It should work for
all types where value-initialization returns the "zero"
value. Now use it like this:

template <class T>
T foo()
{
T retVal(ZeroTraits<T>::zero());

//Just for an example
retVal += 10;

return retVal;

}

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ 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
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 = {};

It can be accessed by u.t then. In both cases, the object is value
initialized, being a sensible default: constructor call for class types with
a declared constructor, and recursive initialization with zero / false for
non-class types.


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

From: Florian Weimer on
> Is there a way to initialize the val?

Wouldn't this work?

T retVal(T());

The compiler should elide the copy-constructor call.

{ Note: this declares a function named retVal. -mod/sk }

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