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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:10:18+00:00 2026-06-08T00:10:18+00:00

Lets consider a simple class template< typename T > class Wrapper { public: //

  • 0

Lets consider a simple class

template< typename T >
class Wrapper {
public:
  // Constructors?
private:
  T wrapped;
};

What constructors should it use to be effective?


Before C++0x there would be a constructor that takes either:

  1. const reference (T const&) – if type T is “heavy”,
  2. or value (T) – if type T is “light”.

Determining whether type T is “heavy” or “light” is not easy.

One could assume only build-in types (ints/floats/…) are “light”. But that is not fully correct since our own Wrapper<int> most likely should be considered a “light” type as well.

Libraries like boost::call_traits provide some means to overcome this difficulty by allowing type creator to mark the type as “light” (by providing proper call_traits specialization). Otherwise it will be treated as “heavy”. Seems acceptable.


But C++0x makes it worse. Because now you have also rvalue reference (T&&) which allows to efficiently take (some) “heavy” objects.

And because of this now you have to chose among:

  1. just const reference (T const&) – if type T is “heavy” and does not support move semantics (because either there is none – like with large PODs – or none was written and you have no influence on that),
  2. both const reference (T const&) and rvalue reference (T&&) – if type T is “heavy” and does support move semantics,
  3. just value (T) – if type T is “light” or if it is “heavy” but supports move semantics (even if copy is made it doesn’t bother use since otherwise we would have to copy from T const& ourselves anyway…).

Still its not easy to tell which types are “heavy” and which are “light” (as previously). But now you are also unable to tell whether type T supports move semantics or not (or are you?).


This becomes even more annoying once you wrap more than one value since number of possible constructor overloads grows exponentially.

Is there any solution to that problem?

I though about some template constructors for forwarding (perfect forwarding) arguments but I wasn’t sure whether that would work as desired. And also it would allow to provide values of different type that would be just forwarded to T constructor. This might be considered a feature but does not have to.

  • 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-08T00:10:19+00:00Added an answer on June 8, 2026 at 12:10 am

    On the contrary, C++11 makes it easier thanks to universal references:

    template <typename T> struct Wrapper
    {
        T value;
    
        template <typename U> Wrapper(U && u)
        : value(std::forward<U>(u))
        {  }
    };
    

    As an extra nice touch, you should add a defaulted second argument that only exists when T is constructible from U, so as to not make your class itself appear constructible from unmatching types. And make it variadic, too:

    template <typename ...Args>
    Wrapper(Args &&... args,
            typename std::enable_if<std::is_constructible<T, Args...>::value, int>::type = 0)
    : value(std::forward<Args>(args)...)
    {  }
    

    Make sure to #include <utility> for forward and #include <type_traits> for the traits.

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

Sidebar

Related Questions

Consider a following chunk of service: public class ProductService : IProductService { private IProductRepository
To give a simple example, consider a Place class: public class Place { //fields
Let's say I have this simple structure class FooDefinition { public FooDefinition Parent {
Lets consider a simple table say products in Oracle (I tried on Oracle 9i).
For my question lets consider the following sample table data: ProductID    ProductName    Price   Category 1                Apple                 5.00       Fruits
Lets consider the following linq statement. var ctx = new MoviesContext(); var movies =
For the scenario lets consider Car to be the highest SWF(stage) inside I load
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
let's consider two views that use the same layout composed of: A left column
Let's consider the following enum in C# public enum ScrollMode : byte { None

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.