Finding constancy of member function - c++

How can I detect a member function has const modifier or not?
Consider the code
struct A {
int member();
int member() const;
};
typedef int (A::*PtrToMember)();
typedef int (A::*PtrToConstMember)() const;
I need something like this:
std::is_const<PtrToMember>::value // evaluating to false
std::is_const<PtrToConstMember>::value // evaluating to true

There you go:
#include <type_traits>
#include <iostream>
#include <vector>
template<typename T>
struct is_const_mem_fn {
private:
template<typename U>
struct Tester {
static_assert( // will always fail
std::is_member_function_pointer<U>::value,
"Use member function pointers only!");
// if you want to report false for types other than
// member function pointers you can just derive from
// std::false_type instead of asserting
};
template<typename R, typename U, typename...Args>
struct Tester<R (U::*)(Args...)> : std::false_type {};
template<typename R, typename U, typename...Args>
struct Tester<R (U::*)(Args...) const> : std::true_type {};
public:
static const bool value =
Tester<typename std::remove_cv<T>::type>::value;
};
struct A {
int member();
int member() const;
};
typedef int (A::*PtrToMember)();
typedef int (A::*PtrToConstMember)() const;
int main()
{
std::cout
<< is_const_mem_fn<PtrToMember>::value
<< is_const_mem_fn<const PtrToMember>::value
<< is_const_mem_fn<PtrToConstMember>::value
<< is_const_mem_fn<const volatile PtrToConstMember>::value
<< is_const_mem_fn<decltype(&std::vector<int>::size)>::value;
}
Output: 00111
EDIT: There's a corner case I forgot to account for in the original answer.
The trait above will choke on a hypothetical member function like this:
struct A {
int member(int, ...) const;
};
because there is no valid specialization of Tester that can be generated for such signature. To fix it, add the following specializations:
template<typename R, typename U, typename...Args>
struct Tester<R (U::*)(Args..., ...)> : std::false_type {};
template<typename R, typename U, typename...Args>
struct Tester<R (U::*)(Args..., ...) const> : std::true_type {};

Below is a simple type trait adapted from here that should allow this.
template <typename T>
struct is_const_mem_func : std::false_type { };
template <typename Ret, typename Class, typename... Args>
struct is_const_mem_func<Ret (Class::*)(Args...) const> : std::true_type { };

Related

Check if a type is the same as a templated type

I have a user defined class
template<typename T, int N>
class MyClass
{
// Implementation
};
and I want to check on the instantiation of another class if its template parameter is an instance of MyClass
template<typename T, std::enable_if_t<!is_MyClass<T>, bool> = true>
class MapClass
{
// custom stuff here
};
template<typename T, std::enable_if_t<is_MyClass<T>, bool> = true>
class MapClass
{
// Some more stuff here
};
I tried to implement it like this but my instantiation fails because it requires two parameters. How do I make automatically extract both parameters
template <typename T> struct is_MyClass : std::false_type {};
template <typename T, int N> struct is_MyClass<MyClass<T, N>> : std::true_type {};
Thanks
I suggest to write a trait is_instantiation_of_myClass that uses partial specialization:
template<typename T, int N>
class MyClass {};
template <typename C>
struct is_instantiation_of_myClass : std::false_type {};
template <typename T,int N>
struct is_instantiation_of_myClass<MyClass<T,N>> : std::true_type {};
template <typename C>
constexpr bool is_instantiation_of_myClass_v = is_instantiation_of_myClass<C>::value;
Now you can do SFINAE based on is_instantiation_of_myClass<T> or just plain specialization:
template <typename T,bool = is_instantiation_of_myClass_v<T>>
struct Foo;
template <typename T>
struct Foo<T,true> {
static constexpr bool value = true;
};
template <typename T>
struct Foo<T,false> {
static constexpr bool value = false;
};
int main() {
std::cout << Foo< int >::value << "\n";
std::cout << Foo< MyClass<int,42>>::value << "\n";
}
Live Demo

How can I check whether a member function has const overload?

Lets say I have
struct foo {
void ham() {}
void ham() const {}
};
struct bar {
void ham() {}
};
Assuming I have a templated function, can I tell whether given type has a const overload for ham?
With
#define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \
template <typename U> \
class traitsName \
{ \
private: \
template<typename T, T> struct helper; \
template<typename T> \
static std::uint8_t check(helper<signature, &funcName>*); \
template<typename T> static std::uint16_t check(...); \
public: \
static \
constexpr bool value = sizeof(check<U>(0)) == sizeof(std::uint8_t); \
}
DEFINE_HAS_SIGNATURE(has_ham_const, T::ham, void (T::*)() const);
And then
static_assert(has_ham_const<foo>::value, "unexpected");
static_assert(!has_ham_const<bar>::value, "unexpected");
Demo
SFINAE over and over again. Here is another option which is unspecified about the return types but lets you specify the arguments.
(For comparison: the approach by #Jarod42 checks the exact signature, return type + arguments, the other void_t expression sfinae stuff up to now checks only whether ham() can be called.)
Plus, it works with the current MSVC 2015 Update 1 version (unlike the usual void_t stuff).
template<typename V, typename ... Args>
struct is_callable_impl
{
template<typename C> static constexpr auto test(int) -> decltype(std::declval<C>().ham(std::declval<Args>() ...), bool{}) { return true; }
template<typename> static constexpr auto test(...) { return false; }
static constexpr bool value = test<V>(int{});
using type = std::integral_constant<bool, value>;
};
template<typename ... Args>
using is_callable = typename is_callable_impl<Args...>::type;
Use it as
struct foo
{
void ham() {}
void ham() const {}
int ham(int) const {}
};
int main()
{
std::cout
<<is_callable<foo>::value //true
<<is_callable<const foo>::value //true
<<is_callable<const foo, int>::value //true
<<is_callable<const foo, double>::value //also true, double is converted to int
<<is_callable<const foo, std::string>::value //false, can't call foo::ham(std::string) const
<<std::endl;
}
Demo on Coliru
For the "newest" sfinae stuff, however, I suggest you have a look at boost.hana.
Detector (like is_detected):
template <typename...>
using void_t = void;
template <typename T, template <typename> class D, typename = void>
struct detect : std::false_type {};
template <typename T, template <typename> class D>
struct detect<T, D, void_t<D<T>>> : std::true_type {};
Sample member verifier:
template <typename T>
using const_ham = decltype(std::declval<const T&>().ham());
Test:
static_assert(detect<foo, const_ham>::value, "!");
static_assert(!detect<bar, const_ham>::value, "!");
DEMO
Another option is to simulate void_t(to appear officially in C++17), which makes use of expression SFINAE to make sure your function is call-able on a const instance, regardless of its return type.
#include <iostream>
#include <type_traits>
struct Foo
{
void ham() const;
void ham();
};
struct Bar {
void ham() {}
};
template<typename...>
using void_t = void;
template<typename C, typename = void>
struct has_const_ham: std::false_type{};
template<typename C> // specialization, instantiated when there is ham() const
struct has_const_ham<C, void_t<decltype(std::declval<const C&>().ham())>> :
std::true_type{};
int main()
{
std::cout << std::boolalpha;
std::cout << has_const_ham<Foo>::value << std::endl;
std::cout << has_const_ham<Bar>::value << std::endl;
}
EDIT
If you want to enforce the return type, then derive the specialization from std::is_same, like
template<typename C> // specialization, instantiated when there is ham() const
struct has_const_ham<C, void_t<decltype(std::declval<const C&>().ham())>> :
std::is_same<decltype(std::declval<const C&>().ham()), void> // return must be void
{};
Live on Coliru
Here is a solution without macros which also does not care about return types:
template <typename T>
struct is_well_formed : std::true_type
{
};
template <typename T, typename = void>
struct has_const_ham : std::false_type
{
};
template <typename T>
struct has_const_ham<T,
typename std::enable_if<is_well_formed<decltype(
std::declval<const T&>().ham())>::value>::type>
: std::true_type
{
};
static_assert(has_const_ham<foo>::value, "oops foo");
static_assert(!has_const_ham<bar>::value, "oops bar");

How to implement is_polymorphic_functor?

I'm trying to implement is_polymorphic_functor meta-function to get the following results:
//non-polymorphic functor
template<typename T> struct X { void operator()(T); };
//polymorphic functor
struct Y { template<typename T> void operator()(T); };
std::cout << is_polymorphic_functor<X<int>>::value << std::endl; //false
std::cout << is_polymorphic_functor<Y>::value << std::endl; //true
Well that is just an example. Ideally, it should work for any number of parameters, i.e operator()(T...). Here are few more test cases which I used to test #Andrei Tita's solution which fails for two test-cases.
And I tried this:
template<typename F>
struct is_polymorphic_functor
{
private:
typedef struct { char x[1]; } yes;
typedef struct { char x[10]; } no;
static yes check(...);
template<typename T >
static no check(T*, char (*) [sizeof(functor_traits<T>)] = 0 );
public:
static const bool value = sizeof(check(static_cast<F*>(0))) == sizeof(yes);
};
which attempts to make use of the following implementation of functor_traits:
//functor traits
template <typename T>
struct functor_traits : functor_traits<decltype(&T::operator())>{};
template <typename C, typename R, typename... A>
struct functor_traits<R(C::*)(A...) const> : functor_traits<R(C::*)(A...)>{};
template <typename C, typename R, typename... A>
struct functor_traits<R(C::*)(A...)>
{
static const size_t arity = sizeof...(A) };
typedef R result_type;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<A...>>::type type;
};
};
which gives the following error for polymorphic functors:
error: decltype cannot resolve address of overloaded function
How to fix this issue and make is_polymorphic_functor work as expected?
This works for me:
template<typename T>
struct is_polymorphic_functor
{
private:
//test if type U has operator()(V)
template<typename U, typename V>
static auto ftest(U *u, V* v) -> decltype((*u)(*v), char(0));
static std::array<char, 2> ftest(...);
struct private_type { };
public:
static const bool value = sizeof(ftest((T*)nullptr, (private_type*)nullptr)) == 1;
};
Given that the nonpolymorphic functors don't have an overloaded operator():
template<typename T>
class is_polymorphic_functor {
template <typename F, typename = decltype(&F::operator())>
static constexpr bool get(int) { return false; }
template <typename>
static constexpr bool get(...) { return true; }
public:
static constexpr bool value = get<T>(0);
};
template<template<typename>class arbitrary>
struct pathological {
template<typename T>
typename std::enable_if< arbitrary<T>::value >::type operator(T) const {}
};
The above functor is non-polymorphic iff there is exactly one T such that arbitrary<T>::value is true.
It isn't hard to create an template<T> functor which is true on int and possibly double, and only true on double if (arbitrary computation returns 1).
So an uncompromising is_polymorphic is beyond the scope of this universe.
If you don't like the above (because it clearly takes more than just int, other types simply fail to find an overload), we could do this:
template<template<typename>class arbitrary>
struct pathological2 {
void operator()(int) const {}
template<typename T>
typename std::enable_if< arbitrary<T>::value >::type operator(T) const {}
};
where the second "overload" is tested, and if there are no T such that it is taken, then the first overload occurs for every single type.

using tr2::direct_bases get nth element of result

struct T1 {};
struct T2: T1 {};
typedef tr2::direct_bases<T2>::type NEW_TYPE ;
should return my something like a touple to bases types. How can I get the nth element
of this __reflection_typelist<...>. I search for something like tuple_element for the reflection list.
You can use this simple metafunction to turn the typelist into an std::tuple:
#include <tr2/type_traits>
#include <tuple>
template<typename T>
struct dbc_as_tuple { };
template<typename... Ts>
struct dbc_as_tuple<std::tr2::__reflection_typelist<Ts...>>
{
typedef std::tuple<Ts...> type;
};
At this point, you could work with it as you would normally work with a tuple. For instance, this is how you could retrieve elements of the type list:
struct A {};
struct B {};
struct C : A, B {};
int main()
{
using namespace std;
using direct_base_classes = dbc_as_tuple<tr2::direct_bases<C>::type>::type;
using first = tuple_element<0, direct_base_classes>::type;
using second = tuple_element<1, direct_base_classes>::type;
static_assert(is_same<first, A>::value, "Error!"); // Will not fire
static_assert(is_same<second, B>::value, "Error!"); // Will not fire
}
Write your own?
template <typename R, unsigned int N> struct get;
template <typename T, typename ...Args, unsigned int N>
struct get<std::tr2::__reflection_typelist<T, Args...>, N>
{
typedef typename get<std::tr2::__reflection_typelist<Args...>, N - 1>::type type;
};
template <typename T, typename ...Args>
struct get<std::tr2::__reflection_typelist<T, Args...>, 0>
{
typedef T type;
};
Or even using first/next:
template <typename R, unsigned int N>
struct get
{
typedef typename get<typename R::next::type, N - 1>::type type;
};
template <typename R>
struct get<R, 0>
{
typedef typename R::first::type type;
};
At this point, I'd say the source code is the best documentation.

Remove reference with const references

For a parametric class C, I want to get always the "primitive" type irrespective of pointer, const or reference modifiers.
template<typename __T>
class C
{
public:
typedef std::some_magic_remove_all<__T>::type T;
}
int main()
{
C<some_type>::type a;
}
For example, for some_type equal to:
int&
int**
int*&
int const &&
int const * const
and so on
I want a is always of type int. How can I achieve it?
If you want to use the standard library more, you can do:
#include <type_traits>
template<class T, class U=
typename std::remove_cv<
typename std::remove_pointer<
typename std::remove_reference<
typename std::remove_extent<
T
>::type
>::type
>::type
>::type
> struct remove_all : remove_all<U> {};
template<class T> struct remove_all<T, T> { typedef T type; };
which removes stuff until that doesn't change the type anymore. With a more recent standard, this can be shortened to
template<class T, class U=
std::remove_cvref_t<
std::remove_pointer_t<
std::remove_extent_t<
T >>>>
struct remove_all : remove_all<U> {};
template<class T> struct remove_all<T, T> { typedef T type; };
template<class T> using remove_all_t = typename remove_all<T>::type;
template<class T> struct remove_all { typedef T type; };
template<class T> struct remove_all<T*> : remove_all<T> {};
template<class T> struct remove_all<T&> : remove_all<T> {};
template<class T> struct remove_all<T&&> : remove_all<T> {};
template<class T> struct remove_all<T const> : remove_all<T> {};
template<class T> struct remove_all<T volatile> : remove_all<T> {};
template<class T> struct remove_all<T const volatile> : remove_all<T> {};
//template<class T> struct remove_all<T[]> : remove_all<T> {};
//template<class T, int n> struct remove_all<T[n]> : remove_all<T> {};
I originally also stripped extents (arrays), but Johannes noticed that this causes ambiguities for const char[], and the question doesn't mention them. If we also want to strip arrays (see also ideas mentioned in the comments), the following doesn't complicate things too much:
#include <type_traits>
template<class U, class T = typename std::remove_cv<U>::type>
struct remove_all { typedef T type; };
template<class U, class T> struct remove_all<U,T*> : remove_all<T> {};
template<class U, class T> struct remove_all<U,T&> : remove_all<T> {};
template<class U, class T> struct remove_all<U,T&&> : remove_all<T> {};
template<class U, class T> struct remove_all<U,T[]> : remove_all<T> {};
template<class U, class T, int n> struct remove_all<U,T[n]> : remove_all<T> {};
or with a helper class but a single template parameter:
#include <type_traits>
template<class T> struct remove_all_impl { typedef T type; };
template<class T> using remove_all =
remove_all_impl<typename std::remove_cv<T>::type>;
template<class T> struct remove_all_impl<T*> : remove_all<T> {};
template<class T> struct remove_all_impl<T&> : remove_all<T> {};
template<class T> struct remove_all_impl<T&&> : remove_all<T> {};
template<class T> struct remove_all_impl<T[]> : remove_all<T> {};
template<class T, int n> struct remove_all_impl<T[n]> : remove_all<T> {};
It is normal if all the variants start looking about the same ;-)
Also you can use the remove_cvref_t function, it has been available since c++20
#include <iostream>
#include <type_traits>
int main()
{
std::cout << std::boolalpha
<< std::is_same_v<std::remove_cvref_t<int>, int> << '\n'
<< std::is_same_v<std::remove_cvref_t<int&>, int> << '\n'
<< std::is_same_v<std::remove_cvref_t<int&&>, int> << '\n'
<< std::is_same_v<std::remove_cvref_t<const int&>, int> << '\n'
<< std::is_same_v<std::remove_cvref_t<const int[2]>, int[2]> << '\n'
<< std::is_same_v<std::remove_cvref_t<const int(&)[2]>, int[2]> << '\n'
<< std::is_same_v<std::remove_cvref_t<int(int)>, int(int)> << '\n';
}