~/okanakyuz
← all posts

Perfect Forwarding with Template

Using a constructor template with a forwarding reference and std::forward, so the right copy or move constructor is called.

8 March 2020updated 25 November 20261 min read

Arrow pointing forward

By writing a constructor template you can cover several alternatives of the constructors.

But there is also a way to call the copy constructor or the move constructor when you forward an object.

In the following code, we provided a Has-A type class to see which constructor is called.

#include <iostream>
#include <string>
#include <typeinfo>
#include <utility>

class HasAClass{
    std::string m_name;
public:
    HasAClass() : m_name{} {
        std::cout << "HasAClass()" << std::endl;
    }
    HasAClass(const std::string &name) : m_name(name) {
        std::cout << "HasAClass(const std::string &name)" << std::endl;
    }
    // a move based constructor
    HasAClass(std::string &&name) : m_name(std::move(name)) {
        std::cout << "HasAClass(std::string &&name)" << std::endl;
    }
    HasAClass(const HasAClass &p) : m_name(p.m_name) {
        std::cout << "HasAClass(const HasAClass &p)" << std::endl;
    }
    // a basic move constructor
    HasAClass(HasAClass &&p) : m_name(std::move(p.m_name)) {
        std::cout << "HasAClass(HasAClass &&p)" << std::endl;
    }
    ~HasAClass(){
        std::cout << "~HasAClass()" << std::endl;
    }
};

class MyClass{
    HasAClass h_c;
public:
    // creates all alternatives
    template<typename T>
    MyClass(T &&s) : h_c(std::forward<T>(s)){
        std::cout << "T is " << typeid(T).name() << std::endl;
    }
};

int main()
{
    // the literal becomes a temporary std::string,
    // so the move constructor is called
    MyClass m_c1{"Move"};

    // for copy constructor
    std::string s{"Copy"};
    MyClass m_c2{s};

    // has a class move constructor
    MyClass m_c3{HasAClass{"has-a-class"}};

    // has a class copy constructor
    HasAClass ha_c{"has-a-class-copy"};
    MyClass m_c4{ha_c};
}

In the Has-A class, we implemented 4 types of initialization, 2 for copy and 2 for move. You may notice that std::move has been used to pass the parameter on without creating a copy of it.

A template constructor has been used in MyClass, and it uses std::forward. T &&s here is not a normal rvalue reference. Because T is deduced, it is a forwarding reference (Scott Meyers calls it a universal reference): it binds to both lvalues and rvalues.

std::forward is a template function which looks at the deduced type T. If T is an lvalue reference type (we passed an lvalue like s), it gives back an lvalue, so the copy constructor is called. Otherwise, it casts the argument to an rvalue, so the move constructor is called. std::forward itself doesn’t copy or move anything; it only keeps the value category of the original argument.

Be careful with this kind of constructor. MyClass m_c5{m_c2}; doesn’t call the copy constructor of MyClass, because for a non-const lvalue the template is a better match. Here it even fails to compile, since there is no way to build a HasAClass from a MyClass.

Update (2026): Since C++20 you can constrain the template with a concept, so it only accepts things HasAClass can be built from and the copy constructor works again:

template<typename T>
    requires std::constructible_from<HasAClass, T>
MyClass(T &&s) : h_c(std::forward<T>(s)) {}

std::constructible_from comes from the <concepts> header.