Skip to content

Instantly share code, notes, and snippets.

@BlueSolei
Created May 18, 2017 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlueSolei/bca26a8590265492e2f2760d3cefcf83 to your computer and use it in GitHub Desktop.
Save BlueSolei/bca26a8590265492e2f2760d3cefcf83 to your computer and use it in GitHub Desktop.
LikeConstVersion
#pragma once
template <typename T>
struct NonConst
{
typedef T type;
};
template <typename T>
struct NonConst<T const>
{
typedef T type;
}; // by value
template <typename T>
struct NonConst<T const&>
{
typedef T& type;
}; // by reference
template <typename T>
struct NonConst<T const*>
{
typedef T* type;
}; // by pointer
template <typename T>
struct NonConst<T const&&>
{
typedef T&& type;
}; // by rvalue-reference
template <typename TConstReturn, class TObj, typename... TArgs>
typename NonConst<TConstReturn>::type
LikeConstVersion(TObj const* obj, TConstReturn (TObj::*memFun)(TArgs...) const, TArgs&&... args)
{
return const_cast<typename NonConst<TConstReturn>::type>((obj->*memFun)(std::forward<TArgs>(args)...));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment