dimanche 23 décembre 2018

Template constructor and copy constructor

Why is the template constructor called instead of the copy constructor in the following code, When I remove 'const' in the copy constructor, it will be called.

#include <iostream>

struct foo{
    int a_;
    template <typename T>
    foo(T&& v) : a_{static_cast<int>(v)}
    {
    }

    foo(const foo& rhs) : a_{rhs.a_}
    {
    }

    operator std::uint8_t(){return a_;}
};


int main()
{
    foo bar1{256}, bar2{bar1};

    if(bar1.a_ == bar2.a_)
        std::cout << "hello!" << std::endl;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire