I dont really know if its worth the struggle and this solution is for sure not perfect as we have to use a helper class Object<T>
, but atleast it works very similiar to your requested behaviour:
#include <iostream>
#include <type_traits>
template<typename T>
using Object = typename std::conditional<std::is_const<T>::value, typename T::const_type, typename T::type>::type;
template<bool IsConst>
struct FooHelper;
struct Foo
{
typedef FooHelper<false> type;
typedef const FooHelper<true> const_type;
protected:
Foo(bool isConst)
{
if (isConst)
std::cout << "const" << std::endl;
else
std::cout << "non-const" << std::endl;
}
};
template<>
struct FooHelper<false> : public Foo
{
FooHelper() : Foo(false) {}
};
template<>
struct FooHelper<true> : public Foo
{
FooHelper() : Foo(true) {}
};
int main()
{
Object<Foo> obj;
Object<const Foo> cobj;
}