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

  • Home
  • SEARCH
  • 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 6014531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:42:20+00:00 2026-05-23T02:42:20+00:00

Occasionaly I find that I need a generic pass through-interface. For example for mix-in

  • 0

Occasionaly I find that I need a generic “pass through”-interface. For example for mix-in classes that forward their constructor arguments to the parent. Or for factory methods that forward their arguments to new:

template<typename C>
C * Create();

template<typename C, typename Arg0>
C * Create(Arg0 & a);

template<typename C, typename Arg0, typename Arg1>
C * Create(Arg0 & a, Arg1 & arg1);

template<typename C, typename Arg0, typename Arg1, typename Arg2>
C * Create(Arg0 & a, Arg1 & arg1, Arg2 & arg2);

When putting the idea into practice I find that it usually works.

However, there are a few problems:

  • When passing the template arguments by value then sometimes reference-types drop their reference-ness along the way.

  • My attempted fix was so pass all arguments as non-const reference. This works for most of the cases because constness is preserved in the type itself and arguments that are passed by value are allowed to be converted to reference.

  • However, a notable exception is temporary const references. See my edit below for more info.

All this however is only my personal experience. I haven’t seen this approach much in other codebases. Because of this I am interested to learn your ideas and insights on above issues.

My main questions are:

  • What are the general guidelines and best practices when designing this kind of interface?

  • How can I avoid losing type information? (Can it be avoided at all?)

  • Can you recommend any good examples of such a design?

Update

In response to @Ben Voigt’s comment. It seems he on to something. It works with a const char * string that is bound to an lvalue, but it doesn’t work when passing the result of typeinfo’s name() method:

template<typename Arg0>
void Create(Arg0 & a) {}

void foo()
{
    // works fine
    const char * typeName = typeid(int).name();
    Create(typeName);

    // error: invalid initialization of non-const reference of type
    // ‘const char*&’ from a temporary of type ‘const char*’
    Create(typeid(int).name());
} 
  • 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-23T02:42:21+00:00Added an answer on May 23, 2026 at 2:42 am

    It’s no big deal, really. You pass by reference to const, and where a reference to non-const is to be passed you require the caller to write ref(x). And instead of definining umpteen versions you define a supporting ArgPack template once and for all.

    Or, use C++0x purrfect führwarding.

    Cheers & hth.,

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

Sidebar

Related Questions

In a database that has over 1 million entries, occasionally we need to find
Looking back over my code I find that I occasionaly have written: ResultSet rs
I find myself writing delegates occasionally for really simple functions (take no arguments and
Occasionally I come accross a unit test that doesn't Assert anything. The particular example
I have an application that is used in image processing, and I find myself
I want to find a minimal set of headers, that work with all caches
I've used jqUploader for a while but I find that it will occasionally fail
Being a programmer I occasionally find the need to analyze large amounts of data
I find I quite seldom come across situations where I need to use the
I need a quick way to find out if a given port is open

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.