Template return type with default value

So, having written a C ++ template class, I defined a method that returns an object of a template type, as such:

template <typename T> class Foo { public: T GetFoo() { T value; //Do some stuff that might or might not set the value of 'value' return value; } }; int main() { Foo<int> foo; foo.GetFoo(); return 0; } 

This gives the following warning:

 prog.cpp: In member function 'T Foo<T>::GetFoo() [with T = int]': prog.cpp:15: warning: 'value' is used uninitialized in this function 

I understand why this is happening. I return an uninitialized int as part of GetFoo . The fact is that if I used Foo<SomeClass> , the string is T value; would initialize value using the default SomeClass .

I was able to suppress this warning by doing the following:

  T GetFoo() { T value = T(); //Do some stuff that might or might not set the value of 'value' return value; } 

This is similar to primitive types (like int and float ) and classes, at least as long as this class has a default constructor and a copy constructor. Is my question a common way to solve this problem? Are there any side effects of this that I should know about?

+4
c ++ templates
source share
6 answers

It sounds fine, if the class does not have a copy constructor, you still cannot return it.

+5
source share

Indeed, the standard way to solve the problem. I do not believe that in this case there should be no side effects.

+3
source share

Line

 T t; 

By default, objects are created, but declares uninitialized built-in types. The syntax "default construct" for the local variable is missing.

Therefore, it is not trivial to write general code that initializes a variable, whether it be an inline or a class.

Including a type in a member variable, however, can lead to a workaround that does not require a copy (except for the return statement):

 template< typename T > struct Initializer { T t; Initializer() :t() // ====> default construction, works for classes _and_ built-in {} }; 

Using this shell, you can create your code in general form:

 template<typename T> T foo() { Initializer<T> i; // fill in it to your liking return it; } 

See full snippet in codepad .

+3
source share

This is similar to primitive types (e.g. int and float) and classes, at least as long as this class has a copy constructor

For classes, not just the [public] copy-constructor, you also need the default public constructor!

Is my question a common way to solve this problem?

It is generally recommended that you create a default public constructor. STL containers use it all the time! If you don't have one, most (or maybe all) STL containers will not work in many cases.

See this example with the default private constructor: http://ideone.com/EdPLu

+1
source share

Are you sure you want to return an uninitialized value from this function? Looks like it could lead to a whole series of crap later down the line.

Why don't you use the appropriate shell - if you have access to boost, consider boost::optional . That way, if you do not initialize it, it can be tested correctly.

+1
source share

Boost addresses this problem in one of the classes of this class of utility templates: boost :: value_initialized and its relatives.

0
source share

All Articles