C++ deduce member function parameters - c++

I'm looking to extend the functionality described here but for member functions, what would be the syntax in this case?
Also, the (*) in the template definition, is that de-referencing the function pointer so the compiler can deduce the template arguments?
Would appreciate any input!
Thanks
template <class F> struct ArgType;
template <class R, class T>
struct ArgType<R(*)(T)> {
typedef T type;
};
void f(int) {}
#include <type_traits>
#include <iostream>
int main() {
// To prove
std::cout << std::is_same< ArgType<decltype(&f)>::type, int >::value << '\n';
// To use
ArgType<decltype(&f)>::type a;
}

Pointer-to-members look like Ret (Cls::*)(Args...) [cv-qualifiers] [ref-qualifiers]. So you can extend your class to deduce the first type thusly:
template <class F> struct ArgType;
template <typename Ret, typename Cls, typename T, typename... Args>
struct ArgType<Ret (Cls::*)(T, Args...)> {
using type = T;
};
Note that you can make this more generic by write a metafunction that gives you the nth argument:
template <typename Ret, typename Cls, typename... Args>
struct ArgType<Ret (Cls::*)(Args...)> {
template <size_t N>
struct get_arg {
using type = typename std::tuple_element<N,
std::tuple<Args...>>::type;
};
};
So ArgType<F>::arg<0>::type would be the type you seek.

Related

Pull Apart Function Type With Specialized Function

The answer to this question picks apart a function type using a class template:
template <typename T>
struct function_args {};
template <typename R, typename... Args>
struct function_args<R(Args...)> {
using type = tuple<Args...>;
};
template <typename T>
using decltypeargs = typename function_args<T>::type;
As I studied what was being done here I tried to rewrite function_args. I attempted to do this using a function so as to eliminate the need for the decltypeargs template. But found myself mired in improper syntax:
template <typename T>
tuple<> myTry();
template <typename Ret, typename... Args>
tuple<Args...> myTry<Ret(Args...)>();
My hope had been to call decltype(myTry<decltype(foo)>()) to get the tuple type instead of having to call decltypeargs<decltype(foo)>. Is there a way to do this with a function declaration?
With a function, you can either just reuse the same type trait from before:
template <typename T>
function_args<T> myTry();
Or you can reimplement the same with functions. You can't partially specialize function templates, but you can overload:
namespace detail {
template <class T> struct tag { };
template <class R, class... Args>
tag<std::tuple<R, Args...>> myTry(tag<R(Args...)> );
template <class R, class C, class... Args>
tag<std::tuple<R, Args...>> myTry(tag<R(C::*)(Args...)> );
// etc.
}
template <typename T>
auto myTry() { return detail::myTry(detail::tag<T>{}); }
//------------------------ Machinery:
#include <tuple>
template< class Ret, class... Args >
std::tuple<Args...> m( Ret(Args...) );
//------------------------- Example:
#include <iostream>
#include <typeinfo>
void foo( double );
using namespace std;
auto main()
-> int
{
using Args_tuple = decltype( m( foo ) );
cout << typeid( Args_tuple ).name() << endl;
}
Functions cannot be specialized like that, but you don't need to specialize a function for this. Tested with gcc 6.1.1:
#include <iostream>
#include <tuple>
template <typename T> struct my_try;
template <typename Ret, typename... Args>
struct my_try<Ret(Args...)> {
std::tuple<Args...> operator()();
};
template<typename T>
auto MyTry()
{
return my_try<T>()();
}
void foo(int, char);
int main()
{
decltype(MyTry<decltype(foo)>()) t;
int *a= &std::get<0>(t);
char *b= &std::get<1>(t);
return 0;
}

Alias for nested variadic templated struct in VC++2013

For a templated nested struct I can define aliases like the following:
template<typename T>struct Struct
{
Struct(T value){}
template<typename T1> struct Nested
{
Nested(T1 value){}
};
};
template<typename T, typename T1>using NameT = struct Struct<T>::Nested<T1>;
using Name = NameT<int, double>; // Alias for a certain instance
Can someone please give me a hint how to declare an alias for the variadic one?
template<typename... T>struct Struct
{
Struct(T... value){}
template<typename T1> struct Nested
{
Nested(T1 value){}
};
};
template<typename... T, typename T1>using NameT = struct Struct<T...>::Nested<T1>; // This seems ok
using Name = NameT<int, double>; // error C976: 'Name': too few template arguments
This will work:
template<typename T1, typename... T>using NameT =
typename Struct<T...>::template Nested<T1>; // This seems ok
^^^^^^^^ ^^^^^^^^
using Name = NameT<int, double>;
Mind though that parameter pack must be at the end so the definition is slightly changed.
LIVE DEMO
Change your code to :
template<typename... T>
struct Struct
{
Struct(T... value){}
template<typename T1>
struct Nested
{
Nested(T1 value){}
};
};
template<typename T1, typename... T>
using NameT = typename Struct<T...>::template Nested<T1>;
using Name = NameT<int, double>;
int main()
{
Name f(1);
}
The variadic template has to be at the end of the template argument list.
#include <type_traits>
#include <tuple>
#include <utility>
template <typename... T>
struct Struct
{
Struct(T... value) {}
template <typename T1>
struct Nested
{
Nested(T1 value) {}
};
};
template <typename, typename>
struct split;
template <typename T, std::size_t... Is>
struct split<T, std::index_sequence<Is...>>
{
using type = typename Struct<typename std::tuple_element<Is, T>::type...>::template Nested<typename std::tuple_element<sizeof...(Is), T>::type>;
};
template <typename... T>
using NameT = typename split<std::tuple<T...>, std::make_index_sequence<sizeof...(T)-1>>::type;
int main()
{
static_assert(std::is_same<NameT<int, double, char>
, Struct<int, double>::Nested<char>>{}, "!");
}
DEMO

Mimic std::function template arguments

I'm want to mimic std::function template arguments but I don't know how it really works.
Consider this code for example:
std::function<int(int)> p;
How do I write class template that mimics this template parameter <int(int)> ?
template<typename ...> <-- what should be here instead of `...`
MyClass
What I'm really trying to achieve is that I want to be able to typedef <int(int)> as function pointer and I want it to be generic not only for int (*func)(int) functions.
I'm trying to achieve something like this:
SomeSmartStruct<MyClass, int(int)>::MemFuncPointerType pMemFunc;
I want MemFuncPointerType to be of the following type:
int (__thiscall MyClass::* )(int)
And:
SomeSmartStruct<MyClass, int(int)>::FunctionPointer pFunc;
should be of this type:
int (__cdecl *)(int)
I'm using VS2010 so not all C++11 features are supported but it does implements std::function.
With variadic templates:
template <typename C, typename T>
struct make_member_function_pointer;
template <typename C, typename R, typename... Args>
struct make_member_function_pointer<C,R(Args...)>
{
using type = R(C::*)(Args...);
};
DEMO 1
Without variadic templates:
template <typename T> struct identity { typedef T type; };
template <typename C, typename T>
struct make_member_function_pointer;
template <typename C, typename R, typename Arg1>
struct make_member_function_pointer<C,R(Arg1)> : identity<R(C::*)(Arg1)> {};
template <typename C, typename R, typename Arg1, typename Arg2>
struct make_member_function_pointer<C,R(Arg1,Arg2)> : identity<R(C::*)(Arg1,Arg2)> {};
template <typename C, typename R, typename Arg1, typename Arg2, typename Arg3>
struct make_member_function_pointer<C,R(Arg1,Arg2,Arg3)> : identity<R(C::*)(Arg1,Arg2,Arg3)> {};
DEMO 2
Usage:
template <typename T, typename F>
struct SomeSmartStruct
{
typedef typename make_member_function_pointer<T,F>::type MemFuncPointerType;
typedef F* FunctionPointer;
};
Tests:
struct MyClass
{
int foo(int) {return 0;}
};
int bar(int) {return 0;}
int main()
{
SomeSmartStruct<MyClass, int(int)>::MemFuncPointerType pMemFunc = &MyClass::foo;
SomeSmartStruct<MyClass, int(int)>::FunctionPointer pFunc = &bar;
}
UPDATE
Can I somehow utilize the preprocessor to auto generate partial specializations of make_member_function_pointer? I've seen something similar is done using BOOST_PP_ITERATION but I don't know how it works.
Sure:
#include <boost/preprocessor.hpp>
template <typename T> struct identity { typedef T type; };
template <typename C, typename T>
struct make_member_function_pointer;
#define BOOST_PP_LOCAL_MACRO(n)\
template <typename C, typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Arg)>\
struct make_member_function_pointer<C,R(BOOST_PP_ENUM_PARAMS(n, Arg))> : identity<R(C::*)(BOOST_PP_ENUM_PARAMS(n, Arg))> {};
#define BOOST_PP_LOCAL_LIMITS (0, 20) // 20 is the limit of params
#include BOOST_PP_LOCAL_ITERATE()
Tests:
int bar10(int,int,int,int,int,int,int,int,int,int) {return 0;}
SomeSmartStruct<MyClass, int(int,int,int,int,int,int,int,int,int,int)>::FunctionPointer pFunc10 = &bar10;
DEMO 3
int(int) is just a type so can be matched by template <typename T>.
Surely too generic:
template<typename T>
struct Pointer
{
typedef T* type;
};
And then you may do
int foo(int i) {return i;}
int main() {
Pointer<int(int)>::type f = &foo;
}

Testing if member function exists using variadics

So I'm very familiar with the paradigm of testing if a member function exists. Currently this code works:
#include <iostream>
#include <type_traits>
struct has_mem_func_foo_impl {
template <typename U, U>
struct chk { };
template <typename Class, typename Arg>
static std::true_type has_foo(chk<void(Class::*)(Arg), &Class::foo>*);
template <typename, typename>
static std::false_type has_foo(...);
};
template <typename Class, typename Arg>
struct has_mem_func_foo : decltype(has_mem_func_foo_impl::template has_foo<Class,Arg>(nullptr)) { };
struct bar {
void foo(int) { }
};
int main() {
static_assert( has_mem_func_foo<bar, int>::value, "bar has foo(int)" );
}
unfortunately if I make a slight adjustment:
#include <iostream>
#include <type_traits>
struct has_mem_func_foo_impl {
template <typename U, U>
struct chk { };
template <typename Class, typename... Arg>
static std::true_type has_foo(chk<void(Class::*)(Arg...), &Class::foo>*);
template <typename, typename...>
static std::false_type has_foo(...);
};
template <typename Class, typename... Arg>
struct has_mem_func_foo : decltype(has_mem_func_foo_impl::template has_foo<Class,Arg...>(nullptr)) { };
struct bar {
void foo(int) { }
};
int main() {
static_assert( has_mem_func_foo<bar, int>::value, "bar has foo(int)" );
}
my static assertion fails. I was under the impression that variadic template parameter packs are treated just the same when expanded into their places. Both gcc and clang produce a failed static assertion.
The real root of my question is thus, is this standard behavior? It also fails when testing for the presence of a variadic templated member function.
The problem I see is that Arg... being passed int is not enough. It would be valid for the compiler to add new args to the end of it.
Deducing what to add to the end of it from nullptr_t isn't possible, so the compiler says "I give up, not this case".
But we don't need to have Arg... in a deducable context for your trick to work:
#include <iostream>
#include <type_traits>
template<class Sig>
struct has_mem_func_foo_impl;
template<class R, class...Args>
struct has_mem_func_foo_impl<R(Args...)> {
template <typename U, U>
struct chk { };
template <typename Class>
static constexpr std::true_type has_foo(chk<R(Class::*)(Args...), &Class::foo>*) { return {}; }
template <typename>
static constexpr std::false_type has_foo(...) { return {}; }
};
template <typename Class, typename Sig>
struct has_mem_func_foo :
decltype(has_mem_func_foo_impl<Sig>::template has_foo<Class>(nullptr))
{};
struct bar {
void foo(int) { }
};
int main() {
static_assert( has_mem_func_foo<bar, void(int)>::value, "bar has foo(int)" );
}
we move the Args... to the class itself, then only pass in the type to the function. This blocks deduction, which makes nullptr conversion to the member function pointer doable, and things work again.
I also included some improved signature based syntax, which also means it supports return type matching.
Note that you may be asking the wrong question. You are asking if there is a member function with a particular signature: often what you want to know is if there is a member function that is invokable with a certain set of arguments, with a return type compatible with your return value.
namespace details {
template<class T, class Sig, class=void>
struct has_foo:std::false_type{};
template<class T, class R, class... Args>
struct has_foo<T, R(Args...),
typename std::enable_if<
std::is_convertible<
decltype(std::declval<T>().foo(std::declval<Args>()...)),
R
>::value
|| std::is_same<R, void>::value // all return types are compatible with void
// and, due to SFINAE, we can invoke T.foo(Args...) (otherwise previous clause fails)
>::type
>:std::true_type{};
}
template<class T, class Sig>
using has_foo = std::integral_constant<bool, details::has_foo<T, Sig>::value>;
which tries to invoke T.foo(int), and checks if the return value is compatible.
For fun, I made the type of has_foo actually be true_type or false_type, not inherited-from. I could just have:
template<class T, class Sig>
using has_foo = details::has_foo<T, Sig>;
if I didn't want that extra feature.

Partial Specialization of tuple contents with variadic arguments

Currently, I'm trying to get some code to react differently to different types. This isn't the exact code, but it gets the message across.
template<class A, class B>
struct alpha {
enum { value = 0 };
};
template<class T, class... Args>
struct alpha<std::tuple<Args...>, T> {
enum { value = 1 };
};
// This gets ignored
template<class T, class... Args>
struct alpha<std::tuple<Args..., std::vector<T> >, T> {
enum { value = 2 };
};
// This gets ignored
template<class T, class... Args>
struct alpha<std::tuple<Args..., T>, T> {
enum { value = 3 };
};
template<class T, class... Args>
struct alpha<T, std::tuple<Args...> > {
enum { value = 4 };
};
template<class... LArgs, class... RArgs>
struct alpha<std::tuple<LArgs...>, std::tuple<RArgs...> > {
enum { value = 5 };
};
int main(int argc, char* argv[]) {
std::cout << alpha<std::tuple<int, double>, double>::value << std::endl; // prints 1
return 0;
}
I've tried more than this code shows, but nothing works so far and I ran across a problem with explicit specialization in a non-namespace scope. For reference, I'm working on gcc 4.6 (the one that comes with oneiric server), which I believe has complete variadic template support. I don't care how ugly it gets if the implementation works to detect the last argument of the parameter pack and the other types as well. Any suggestions?
EDIT:
I wanted to share the solution I used based on the answers (this is an example).
template<typename T> struct tuple_last;
template<typename T, typename U, typename... Args>
struct tuple_last<std::tuple<T,U,Args...>> {
typedef typename tuple_last<std::tuple<U,Args...>>::type type;
};
template<typename T>
struct tuple_last<std::tuple<T>> {
typedef T type;
};
namespace details {
// default case:
template<class T, class U>
struct alpha_impl {
enum { value = 1 };
};
template<class T>
struct alpha_impl<T, T> {
enum { value = 101 };
};
template<class T>
struct alpha_impl<T, std::vector<T>> {
enum { value = 102 };
};
// and so on.
}
template<class T, class... Args>
struct alpha<std::tuple<Args...>, T>
: details::alpha_impl<T, tuple_last<std::tuple<Args...>>;
If you compile using clang, it helpfully reports that (2) and (3) are unusable. The warning for (3), which you expect to be selected, is as follows:
warning: class template partial specialization contains a template parameter that can not be deduced; this partial specialization will never be used
struct alpha<std::tuple<Args..., T>, T> {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: non-deducible template parameter 'Args'
template<class T, class... Args>
^
Why is Args not deducible? The C++0x FDIS states at ยง14.8.2.5/9:
If the template argument list of [a type that is specified in terms of template parameters] contains a pack expansion that is not the last template argument, the entire template argument list is a non-deduced context.
In your specialization, the type std::tuple<Args..., T> is a type that is specified in terms of template parameters Args and T. It contains a pack expansion (Args...), but that pack expansion is not the last template argument (T is the last template argument). Thus, the entire template argument list of the tuple (the entirety of <Args..., T>) is a non-deduced context.
The argument list of the std::tuple is the only place in the template specialization's argument list that Args appears; since it is not deducible from there, it is not deducible at all and the specialization will never be used.
Matthieu M. provides a clever workaround in his answer.
#James provided the why, now let's try to find an alternative.
I would suggest using another level of indirection.
1. Getting the last argument
template <typename T> struct Last;
template <typename T, typename U, typename... Args>
struct Last<std::tuple<T,U,Args...>>
{
typedef typename Last<std::tuple<U,Args...>>::type type;
};
template <typename T>
struct Last<std::tuple<T>>
{
typedef T type;
};
2. Introducing a specialized helper
template <typename T, typename U>
struct alpha_tuple
{
enum { value = 1 };
};
template <typename T>
struct alpha_tuple<T,T>
{
enum { value = 3 };
};
template <typename T>
struct alpha_tuple<std::vector<T>,T>
{
enum { value = 2; }
};
3. Hooking it up
template <typename T>
struct alpha<std::tuple<>, T>
{
enum { value = 1 };
};
template <typename T, typename U, typename Args...>
struct alpha<std::tuple<U, Args...>, T>
{
typedef typename Last<std::tuple<U, Args...>>::type LastType;
enum { value = alpha_tuple<LastType,T>::value };
};
Note that there is no last type for empty tuples, so I had to deal with them in a separate specialization.
If you like to find out whether a tuple as a specific last member, here's a type trait for that:
#include <type_traits>
#include <tuple>
template <typename ...Args> struct back;
template <typename T, typename ...Args> struct back<T, Args...>
{ typedef typename back<Args...>::type type; };
template <typename T> struct back<T>
{ typedef T type; };
template <typename...> struct tuple_has_last : public std::false_type {};
template <typename T, typename... Args> struct tuple_has_last<T, std::tuple<Args...>>
{
static const bool value = std::is_same<typename back<Args...>::type, T>::value;
};
Edit: Oh, I didn't see that Matthieu had already written the exact same thing. Never mind.