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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:50:57+00:00 2026-05-15T13:50:57+00:00

Consider the following function template: template<typename T> void Foo(T) { // … } Pass-by-value

  • 0

Consider the following function template:

template<typename T> void Foo(T)
{
  // ...
}

Pass-by-value semantics make sense if T happens to be an integral type, or at least a type that’s cheap to copy.
Using pass-by-[const]-reference semantics, on the other hand, makes more sense if T happens to be an expensive type to copy.

Let’s assume for a second that you are writing a library. Ideally, as a library implementer, your job is to provide your consumers with a clean API that is both as generic and efficient as possible. How then, do you provide a generic interface that caters to both types of argument passing strategies?


Here is my first attempt at getting this to work:

#include <boost/type_traits.hpp>

template<typename T> struct DefaultCondition
{
  enum {value = boost::is_integral<T>::value /* && <other trait(s)> */};
};

template< typename T, class Condition = DefaultCondition<T> > class Select
{
  template<bool PassByValue = Condition::value, class Dummy = void> struct Resolve
  {
    typedef T type;
  };

  template<class Dummy> struct Resolve<false, Dummy>
  {
    typedef const T& type;
  };

  public: typedef typename Resolve<>::type type;
};

Typical usage:

template<typename T> class EnterpriseyObject
{
  typedef typename Select<T>::type type;

  public: explicit EnterpriseyObject(type)
  {
    // ...
  }
};

struct CustomType {};

void Usage()
{
  EnterpriseyObject<int>(0); // Pass-by-value.
  (EnterpriseyObject<CustomType>(CustomType())); // Pass-by-const-reference.
}

This, of course, indirectly breaks implicit template argument deduction for non-class templates:

template<typename T> void Foo(typename Select<T>::type)
{
  // ...
}

void Usage()
{
  Foo(0);      // Incomplete.
  Foo<int>(0); // Fine.
}

This can be “fixed” with the Boost.Typeof library and a macro, a la the WinAPI:

#define Foo(Arg) ::Foo<BOOST_TYPEOF((Arg))>((Arg))

Though this is just a quasi-portable hack.

As you can see, my general approach is not really satisfactory for all cases.


As a hobbyist programmer, I neither have real-world experience nor do I have access to production-quality code for reference. I also realize that this might seem like a bad case of premature optimization, but I’m genuinely interested in a couple of things:

  1. Do you, or have you used this type of optimization* in the past?
  2. Does the Boost (or any other public) library already provide similar functionality?
  3. If the answer to #1 or #2 is a ‘yes’ — how is the non-class template case handled?
  4. Are there any obvious pitfalls that I’m not seeing with something like this?
  5. Finally, is this even a sane thing to do?

* Not profiled. 😉

  • 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-15T13:50:57+00:00Added an answer on May 15, 2026 at 1:50 pm
    1. Yes. All the time. I use it myself.
    2. Yes, use Boost.Utility’s Call Traits 🙂

      Usage would be…

      template <typename T>
      void foo(boost::call_traits<T>::param_type param)
      {
          // Use param
      }
      
    3. As far as I know, non-class templates are passed-by-value unless it is faster to not. Thanks to partial template specialization, it can be customized relatively easily.

    4. Sorry, didn’t really read what you did, it just looked like exactly what I went through a few months ago. Therefore, can’t really answer this one. My recommendation is just to read through Boost.Utility.

    5. Of course!

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

Sidebar

Related Questions

Consider the following setup: I am given an interface template<class T> void FooClass<T>::foo(boost::function<double (int)>
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
Consider the following function: public function foo(bar1:int, bar2:uint, bar3:String, bar4:Boolean):void{} What I want is
Consider the following code: struct Undefined; template <typename T> void TemplateFunction() { Undefined obj;
Please consider the following code. struct foo { }; template<typename T> class test {
Consider the following: template <typename T> class testString { public: typedef T* iterator; void
Consider the following function: void f(const char* str); Suppose I want to generate a
Consider the following code: $(window).unload(function () { console.log('foo'); }); If I trigger the unload
Consider the following class: class Foo { enum Flags {Bar, Baz, Bax}; template<Flags, class

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.