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 3666594
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:53:58+00:00 2026-05-19T01:53:58+00:00

I have a templated class that is dependent on two template parameters to calculate

  • 0

I have a templated class that is dependent on two template parameters to calculate its values but only one for construction. I would like to relieve the dependency on the second over its lifetime. I am looking at doing this by using a templated static function to calculate the values and then create an instance. All of the members inside this class are const POD types and there will be a good many of this class being created at application start up.

template < class member >
class FOO {
public:
 FOO() : a(7) {};

 template < class scope >
 static FOO CreateFOO()
 {
  return FOO();
 };

private:
 const int a;
};

template < class member, class scope >
const FOO< member >* function()
{
 static const FOO< member >& temp = FOO< member >::CreateFOO< scope >();

 return &temp;
}

int main() {

 const FOO<int>* b = function< int, int >();

 return 0;
}

I contemplated move semantics but because I am simply containing const POD types there really isn’t any swapping and moving simply copying and destroying. Is the above reasonable/valid? Everything I have read suggests it is ok! Is there a better way to do this?

  • 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-19T01:53:58+00:00Added an answer on May 19, 2026 at 1:53 am

    This should be okay. Since the reference temp is bound to the temporary object, the lifetime of the temporary is extended to the lifetime of temp, which extends past the end of main().

    But I don’t see any real reason to use a reference there. Why not just:

    {
        static const FOO<member> foo = FOO<member>::CreateFOO<scope>();
        return &foo;
    }
    

    Your compiler can probably optimize away the copy constructor. And if not, you say it’s all POD members, so no big deal.

    Either way, you might worry about the static deinitialization order fiasco. Or maybe you’re sure they’ll never be used from destructors.

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

Sidebar

Related Questions

I have a templated container class that looks something like this: template <class ItemType>
I have a templated class that I want to avoid copying (because of the
I want to have a template class that looks something like what I have
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have a custom iterator template class that wraps up a std::list iterator. In
I have a template class with a variadic template member function that I am
I have the following use case: There's a class called Template and with that
I have code that looks like this: template<class T> class list { public: class
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
I have two classes. One is a management class, which stores a bunch of

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.