I'm writing a class where I have a templated constructor and copy constructor. Every time I want to call copy constructor with non const object, templated constructor gets chosen. How can I force compiler to choose copy constructor?
Here is the mcve:
#include <iostream>
struct foo
{
foo()
{
std::cout << "def constructor is invoked\n";
}
foo(const foo& other)
{
std::cout << "copy constructor is invoked\n";
}
template <typename T>
foo(T&& value)
{
std::cout << "templated constructor is invoked\n";
}
};
int main()
{
foo first;
foo second(first);
}
Deleting a function is not what I want.
const &foo
when calling the ctor do he job? The ctor is for const args, so provide one. – Digitalstd::variant
. I don't think people will like casting. I want to keep user side clean – HeadmostT
that is anything likefoo
? (foo&&
,foo&
,const foo&
,volatile foo&
, ...). This should be easy enough with a littleenable_if
. Or merely ban the template constructor in the particular cases where there is a viable non-template constructor? (That latter isn't possible, I think) – Wishywashystd::variant
supports templated constructor without dummy first parameter. I want to write the implementation which conforms standard as strictly as possible. – Headmoststd::enable_if< std::is_same< std::decay_t< T >, variant >::value >
). Also you may usestd::as_const()
-function (C++17, libc++) to cast lvalue reference toconst
one. But my first assertion is essential, I sure. – Dollop