Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4055606
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:43:21+00:00 2026-05-20T14:43:21+00:00

So in writing a C++ template class, I have defined a method that returns

  • 0

So in writing a C++ template class, I have defined a method that returns an object of the templated 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 am returning an uninitialized int as part of GetFoo. The thing is, if I were to use Foo<SomeClass>, the line T value; would initialize value using the default constructor of SomeClass.

I have managed 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 seems to work for primitive types (such as int and float) and classes, at least so long as that class has a default constructor and copy constructor. My question is – is this the accepted way of solving this problem? Are there any side effects of this I should know about?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:43:21+00:00Added an answer on May 20, 2026 at 2:43 pm

    Sounds OK, if the class has no copy constructor, you will not be able to return it anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a templated C++ generic container class that can optionally maintain its contents
I'm writing a simple maths library with a template vector type: template<typename T, size_t
I have a template class A, and a stream operator function for A that
I'm writing some template classes for parseing some text data files, and as such
I am writing some templated pure virtual base classes that are multiply inherited and
I`m writing a chat using WinSock2 and WinAPI functions. And I have a little
I have this code which compiles and works as expected: class Right {}; class
I have a class (for logging) which derives from std::ostream. Stripped down, it looks
I have a c++ class which exposes collections by providing functions returning ranges, using
I have a class called 'subscribedQueue'. This class receives its data via its subscribed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.