Well, if your compiler is MSVC, this will work:
template<class T>
struct A {
A(const T & x) {}
A(const A&) = delete;
A(A&&) = delete;
};
template <typename T>
A<T> create_A(const T& t)
{
return t;
}
const auto& a = create_A(666);
const auto& b = create_A(a);
const auto& c = create_A(b);
No such luck with clang and g++, though.
Assigning a result returned by value to a const reference is perfectly legal, by the way, and has its uses. Why MSCV avoids checking for type being moveable/copyable (even though it would optimise it away) is a mystery to me and likely a bug. But, it'd kinda work in your case if you need to do it this way.
EDIT: alternatively, if you are not afraid to bring the wrath of C++ gods upon yourself, you can transform create_A
into a macro:
#define create_A(x) (A<decltype(x)>(x))
Now it shall work on all compilers.
EDIT2: as @dyp suggested, this answer can be improved further:
template <typename T>
A<T> create_A(const T& t)
{
return { t };
}
auto&& a = create_A(666);
auto&& b = create_A(a);
auto&& c = create_A(b);
It will work on all C++11 compilers.