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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:37:23+00:00 2026-06-14T23:37:23+00:00

I need a way to decide whether to pass an arbitrary type T by

  • 0

I need a way to decide whether to pass an arbitrary type T by copy or by const-reference (e.g., if T is small enough then copy if, otherwise pass it by const-reference). To avoid reinventing the wheel, I tried out Boost Call Traits.

As expected, primitive types are passed by value, and complex types like std::string are passed by reference. However, tiny non-primitive types are also passed by reference, e.g., std::pair<char, char>, which does not seem to be the best choice. I assumed that everything up to sizeof(void*) would be passed by value.

Generally, the Boost libraries are of high quality, so maybe I am missing something.

Here is my test code:

#include <iostream>
#include <type_traits>
#include <tuple>
#include <boost/call_traits.hpp>

template <typename TYPE>
void test(const char* type_name)
{
  typedef typename boost::call_traits<TYPE>::param_type T;  
  if(std::is_reference<T>::value)        
    std::cout << type_name << " is passed by reference (sizeof=" << sizeof(TYPE) << ")\n"; 
  else 
    std::cout << type_name << " is passed by value (sizeof=" << sizeof(TYPE) << ")\n"; 
}

int main()
{
  test<short>("short");
  test<int>("int");
  test<double>("double");
  test<std::string>("std::string");
  test<long long>("long long");
  test<std::pair<int, int>>("std::pair<int, int>");
  test<std::pair<short, short>>("std::pair<short, short>");
  test<std::pair<char, char>>("std::pair<char, char>");
  test<std::tuple<char, char>>("std::tuple<char, char>");
  test<std::tuple<char, char, char, char>>("std::tuple<char, char, char, char>");
  test<std::pair<long long, long long>>("std::pair<long long, long long>");
  return 0;
}

And here are the results (Boost 1.50, g++ 4.7.2):

short is passed by value (sizeof=2)
int is passed by value (sizeof=4)
double is passed by value (sizeof=8)
std::string is passed by reference (sizeof=8)
long long is passed by value (sizeof=8)
std::pair<int, int> is passed by reference (sizeof=8)
std::pair<short, short> is passed by reference (sizeof=4)
std::pair<char, char> is passed by reference (sizeof=2)
std::tuple<char, char> is passed by reference (sizeof=2)
std::tuple<char, char, char, char> is passed by reference (sizeof=4)
std::pair<long long, long long> is passed by reference (sizeof=16)
  • 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-06-14T23:37:25+00:00Added an answer on June 14, 2026 at 11:37 pm

    Call Traits is a generic library and must work with any user-defined types. To decide whether it is faster to pass an arbitrary type by value requires extra knowledge which is beyond the scope of the library. So, the conservative approach of always passing by value when in doubt, is reasonable.

    Although it should be possible to extend it to improve the support for commonly used STL types (e.g., std::pair, std::tuple, std::array), there is no generic way to do so.

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

Sidebar

Related Questions

Now that RC1 is out I need to decide once and for all whether
I am looking for the fastest way to decide whether or not a point
I need a way to change the specific value of a column of a
I need a way in my Chrome extension to know when the browser is
I need a way to take text from a EditText as soon as the
it need a way to call function whose name is stored in a string
I need a way to update tables on live machine without affecting users experience.
I need a way to know what are the children of an XmlNode allowed
I need a way to write over lines in a specific file. As it
I need a way to automatically format Date and Number objects based on locale

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.