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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:03:36+00:00 2026-05-24T14:03:36+00:00

Lets say I have a function which takes a function pointer as a parameter,

  • 0

Lets say I have a function which takes a function pointer as a parameter, and that parameter has a default argument.

template <typename T>
T* default_construct()
{
    return new T();
}

template <typename T>
void register(T* (*construct)() = default_construct<T>)
{
    // Save that function pointer for later
}

Lets say I want to use register on my class Foo, but Foo doesn’t have a default constructor, so my default_construct won’t work on it. The obvious solution is to do something like this:

Foo* construct_Foo()
{
    return new Foo("String argument", 123);
}

SomeFunc()
{
    // ...
    register<Foo>(construct_Foo);
    // ...
}

But that doesn’t work. Even though register<Foo> may only be called in one place, and it’s passed a function to use, default_construct<Foo> still gets instantiated by the compiler, and I get compiler errors. It seems like since it never gets used, it ought to be skipped over, but I guess that’s not the case.

Is there any way to prevent default_construct from being instantiated when it’s being used as a default argument? The only solution I can think of is to put it in the template, but it seems like there ought to be a better solution.

  • 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-24T14:03:38+00:00Added an answer on May 24, 2026 at 2:03 pm

    Here’s one solution that solves the problem because it doesn’t use default arguments:

    template <typename T>
    T* default_construct()
    {
        return new T();
    }
    
    template <typename T>
    void register(T* (*construct)())
    {
        // Save that function pointer for later
    }
    
    template<typename T>
    void register()
    {
        register<T>(default_construct<T>);
    }
    

    Note that register is a C++ keyword though 🙂

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

Sidebar

Related Questions

Lets say I have a javascript function call moveItem which takes two parameters source
Lets say I have a function where the parameter is passed by value instead
Lets say I have a function that needs to return some integer value. but
Let's say I have a simple class called WebsterDictionary that has a function that
Lets say I have a library function that I cannot change that produces an
Lets say I have a function f[x_, y_] , and two lists l1 ,
Let's say you have a function that returns a date: Date myFunc(paramA, paramB){ //conditionally
I need to expose a function, let's say compute that takes two input files:
This has been bothering me for a long time now: Lets say i have
Lets say I have heap allocated A* , which I want to pass as

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.