I got class with template methods that looks at this:
struct undefined {};
template<typename T> struct is_undefined : mpl::false_ {};
template<> struct is_undefined<undefined> : mpl::true_ {};
template<class C>
struct foo {
template<class F, class V>
typename boost::disable_if<is_undefined<C> >::type
apply(const F &f, const V &variables) {
}
template<class F, class V>
typename boost::enable_if<is_undefined<C> >::type
apply(const F &f, const V &variables) {
}
};
apparently, both templates are instantiated, resulting in compile time error.
is instantiation of template methods different from instantiation of free functions?
I have fixed this differently, but I would like to know what is up.
the only thing I can think of that might cause this behavior, enabling condition does not depend immediate template arguments, but rather class template arguments
Thank you
Your C does not participate in deduction for apply. See this answer for a deeper explanation of why your code fails.
You can resolve it like this:
template<class C>
struct foo {
template<class F, class V>
void apply(const F &f, const V &variables) {
apply<F, V, C>(f, variables);
}
private:
template<class F, class V, class C1>
typename boost::disable_if<is_undefined<C1> >::type
apply(const F &f, const V &variables) {
}
template<class F, class V, class C1>
typename boost::enable_if<is_undefined<C1> >::type
apply(const F &f, const V &variables) {
}
};
Related
Currently I'm looking for some way to get the parameter types of an overloaded member function by argument count and instance type.
In my case we know the name, the return type (void) and the argument count.
In addition there can't be a second overload with the same argument count and the functions are never cv nor ref qualified.
A sample:
#include <tuple>
#include <type_traits>
#include <iostream>
class TupleLikeType{
public:
void Deconstruct(int& a, char& b){
a = 12;
b = 'A';
}
void Deconstruct(int& a, char& b, bool& c){
Deconstruct(a, b);
c = true;
}
};
template<typename Type, std::size_t ArgCount>
class TupleDeconstructTypes{
public:
using type =
//to implement
std::conditional_t<ArgCount == 2,
std::tuple<int&, char&>, std::tuple<int&, char&, bool&>
>
//to implement end
;
};
template<typename T>
class DeconstructAdapter
{
private:
T& obj;
public:
template<
typename DepT = T
, typename... Args
>
constexpr void operator()(Args&&... args)
{
return obj.Deconstruct(std::forward<Args>(args)...);
}
public:
constexpr DeconstructAdapter(T& toDeconstruct) : obj(toDeconstruct)
{
}
};
template<typename Tuple>
class TupleWithOutRefs{};
template<template <typename...> typename Tuple, typename... T>
class TupleWithOutRefs<Tuple<T...>>{
public:
using type = Tuple<std::remove_reference_t<T>...>;
};
template<
std::size_t ArgCount
,typename T
,typename Args = typename TupleDeconstructTypes<T, ArgCount>::type
,typename TempTuple = typename TupleWithOutRefs<Args>::type
>
auto CreateTuple(T& t){
auto adapter = DeconstructAdapter(t);
TempTuple result{};
//std::apply calls std::invoke
//during this call all out-references are bound to our allocated stack memory
std::apply(adapter, result);
return result;
}
int main(){
//usage
static_assert(std::is_same_v<
TupleDeconstructTypes<TupleLikeType, 2>::type,
std::tuple<int&, char&>
>);
TupleLikeType t;
auto tuple = CreateTuple<2>(t);
std::cout << std::get<0>(tuple);
}
Do you have any idea to solve this in a generic way?
With limited number, you can do something like:
template <std::size_t Size>
struct helper;
template <>
struct helper<0>
{
template <typename C>
std::tuple<> operator() (void (C::*) ()) const;
};
template <>
struct helper<1>
{
template <typename C, typename T1>
std::tuple<T1> operator() (void (C::*) (T1)) const;
};
template <>
struct helper<2>
{
template <typename C, typename T1, typename T2>
std::tuple<T1, T2> operator() (void (C::*) (T1, T2)) const;
};
template <>
struct helper<3>
{
template <typename C, typename T1, typename T2, typename T3>
std::tuple<T1, T2, T3> operator() (void (C::*) (T1, T2, T3)) const;
};
// ...
template<typename Type, std::size_t ArgCount>
using TupleDeconstructTypes = decltype(helper<ArgCount>{}(&Type::Deconstruct));
Demo
I have two sets of mixin base classes that follow the following pattern
// base class taking one contructor argument
struct OneArgBase
{
const double x;
template<typename T>
OneArgBase(const T & t) :
x(t.x)
{}
};
// base class taking two constructor arguments
struct TwoArgBase
{
const double y;
template<typename T, typename U>
TwoArgBase(const T & t, const U & u) :
y(t.y + u.y)
{}
};
From these base classes i derive two sets of mixin classes
template<typename ... Mixins>
struct OneArgMix : Mixins...
{
template<typename T>
OneArgsMix(const T & t) :
Mixins(t)...
{}
};
template<typename ... Mixins>
struct TwoArgMix : Mixins...
{
template<typename T, typename U>
TwoArgsMix(const T & t, const U & u) :
Mixins(t, u)...
{}
};
The problem I am facing now, is that i want to pass classes following the OneArgBase pattern to
TwoArgMix
using Mix = TwoArgMix<TwoArgBase, OneArgBase>;
template<typename ... Mixins>
struct TwoArgMix : Mixins...
{
template<typename T, typename U>
TwoArgsMix(const T & t, const U & u) :
Mixins(t, u)... // if Mixins is TwoArgBase
Mixins(t)... // if Mixins is OneArgBase
{}
};
but have no idea how two write the constructor of TwoArgMix in such a way that if passes
only the first template parameter to Mixin base classes that follow the OneArgMix pattern.
If possible i would like to avoid writing dummy arguments to the OneArgMix constructor,
because these classes are needed for the OneArgMix too.
All problems in programming can be solved by adding another layer of indirection.
We need to conditionally ignore the second argument if it's not constructible. One way to do that is to wrap each mixin in another type that conditionally ignores its second argument:
template <typename M>
struct WrappedMixin : M
{
template <typename T, typename U>
WrappedMixin(T const& t, U const& u)
: WrappedMixin(t, u, std::is_constructible<M, T const&, U const&>{})
{ }
private:
template <typename T, typename U>
WrappedMixin(T const& t, U const& u, std::true_type /* yes, use both */)
: M(t, u)
{ }
template <typename T, typename U>
WrappedMixin(T const& t, U const&, std::false_type /* no, just one */)
: M(t)
{ }
};
And now, our main constructor is easy: we just inherit from the wrapped ones instead:
template<typename ... Mixins>
struct TwoArgMix : WrappedMixin<Mixins>...
{
template<typename T, typename U>
TwoArgsMix(const T & t, const U & u)
: WrappedMixin<Mixins>(t, u)...
{ }
};
With factory, you might do something like:
template <typename> struct Tag{};
template<typename T, typename U>
OneArgBase Create(Tag<OneArgBase>, const T& t, const U&)
{
return OneArgBase(t);
}
template<typename T, typename U>
TwoArgBase Create(Tag<TwoArgBase>, const T& t, const U& u)
{
return TwoArgBase(t, u);
}
template<typename ... Mixins>
struct TwoArgsMix : Mixins...
{
template<typename T, typename U>
TwoArgsMix(const T & t, const U & u) :
Mixins(Create(Tag<Mixins>{}, t, u))...
{}
};
Demo
You can try to use partially specified template.
Something like:
template<typename twoarg>
struct TwoArgMix<twoarg, OneArgBase>: ...
It will be specific implementation with OneArgBase as second template parameter.
I have several functions that I would like to work for derived classes of a CRTP base class. The issue is that if I pass the derived classes into the free functions meant for the CRTP class, ambiguities arise. A minimal example to illustrate this is this code:
template<typename T>
struct A{};
struct C : public A<C>{};
struct B{};
template<typename T, typename U>
void fn(const A<T>& a, const A<U>& b)
{
std::cout << "LT, RT\n";
}
template<typename T, typename U>
void fn(const T a, const A<U>& b)
{
std::cout << "L, RT\n";
}
template<typename T, typename U>
void fn(const A<T>& a, const U& b)
{
std::cout << "LT, R\n";
}
int main()
{
C a; // if we change C to A<C> everything works fine
B b;
fn(a,a); // fails to compile due to ambiguous call
fn(b,a);
fn(a,b);
return 0;
}
Ideally I would like this to work for the derived classes as it would if I were to use the base class (without having to redefine everything for the base classes, the whole point of the CRTP idiom was to not have to define fn for multiple classes).
First, you need a trait to see if something is A-like. You cannot just use is_base_of here since you don't know which A will be inherited from. We need to use an extra indirection:
template <typename T>
auto is_A_impl(A<T> const&) -> std::true_type;
auto is_A_impl(...) -> std::false_type;
template <typename T>
using is_A = decltype(is_A_impl(std::declval<T>()));
Now, we can use this trait to write our three overloads: both A, only left A, and only right A:
#define REQUIRES(...) std::enable_if_t<(__VA_ARGS__), int> = 0
// both A
template <typename T, typename U, REQUIRES(is_A<T>() && is_A<U>())
void fn(T const&, U const&);
// left A
template <typename T, typename U, REQUIRES(is_A<T>() && !is_A<U>())
void fn(T const&, U const&);
// right A
template <typename T, typename U, REQUIRES(!is_A<T>() && is_A<U>())
void fn(T const&, U const&);
Note that I'm just taking T and U here, we don't necessarily want to downcast and lose information.
One of the nice things about concepts coming up in C++20 is how much easier it is to write this. Both the trait, which now becomes a concept:
template <typename T> void is_A_impl(A<T> const&);
template <typename T>
concept ALike = requires(T const& t) { is_A_impl(t); }
And the three overloads:
// both A
template <ALike T, ALike U>
void fn(T const&, U const&);
// left A
template <ALike T, typename U>
void fn(T const&, U const&);
// right A
template <typename T, ALike U>
void fn(T const&, U const&);
The language rules already enforce that the "both A" overload is preferred when it's viable. Good stuff.
Given that in your example the first element of the second function and the second element of the third should not inherit from the CRTP you can try something like the following:
#include<iostream>
#include<type_traits>
template<typename T>
struct A{};
struct C : public A<C>{};
struct B{};
template<typename T, typename U>
void fn(const A<T>& a, const A<U>& b)
{
std::cout << "LT, RT\n";
}
template<typename U>
struct isNotCrtp{
static constexpr bool value = !std::is_base_of<A<U>, U>::value;
};
template<typename T, typename U, std::enable_if_t<isNotCrtp<T>::value, int> = 0>
void fn(const T a, const A<U>& b)
{
std::cout << "L, RT\n";
}
template<typename T, typename U, std::enable_if_t<isNotCrtp<U>::value, int> = 0>
void fn(const A<T>& a, const U& b)
{
std::cout << "LT, R\n";
}
int main()
{
C a;
B b;
fn(a,a);
fn(b,a);
fn(a,b);
return 0;
}
Basically we disable the second and third functions when passing a CRTP in first and second argument, leaving only the first function available.
Edit: answering to OP comment, if T and U both inherit the first will be called, wasn't this the expected behavior?
Play with the code at: https://godbolt.org/z/ZA8hZz
Edit: For a more general answer, please refer to the one posted by user Barry
This is one of those situations when it's convenient to create a helper class that can be partially specialized to do this, with the function turned into a wrapper that selects the appropriate specialization:
#include <iostream>
template<typename T>
struct A{};
struct C : public A<C>{};
struct B{};
template<typename T, typename U>
struct fn_helper {
static void fn(const T &a, const U &b)
{
std::cout << "L, R\n";
}
};
template<typename T, typename U>
struct fn_helper<T, A<U>> {
static void fn(const T &a, const A<U> &b)
{
std::cout << "L, RT\n";
}
};
template<typename T, typename U>
struct fn_helper<A<T>, U> {
static void fn(const A<T> &a, const U &b)
{
std::cout << "LT, R\n";
}
};
template<typename T, typename U>
struct fn_helper<A<T>, A<U>> {
static void fn(const A<T> &a, const A<U> &b)
{
std::cout << "LT, RT\n";
}
};
template<typename T, typename U>
void fn(const T &a, const U &b)
{
fn_helper<T,U>::fn(a, b);
}
int main()
{
A<C> a;
B b;
fn(a,a);
fn(b,a);
fn(a,b);
return 0;
}
Output (gcc 9):
LT, RT
L, RT
LT, R
I would expect modern C++ compilers to require selecting only their most modest optimization level to completely optimize away the wrapping function call.
I have a typelist. I would like to create a tuple with the results of calling a function on each type in that list and then use that as arguments to another functor. So something like this:
template<typename F>
struct function_traits;
template<typename T, typename R, typename... Args>
struct function_traits<R(T::*)(Args...) const> {
using return_type = R;
using param_types = std::tuple<Args...>;
};
template<typename T> struct function_traits : public
function_traits<decltype(&T::operator())> {};
template <typename T>
T* get_arg(int id)
{
// Actual implementation omitted. Uses the id parameter to
// do a lookup into a table and return an existing instance
// of type T.
return new T();
}
template <typename Func>
void call_func(Func&& func, int id)
{
using param_types = function_traits<Func>::param_types>;
func(*get_arg<param_types>(id)...); // <--- Problem is this line
}
call_func([](int& a, char& b) { }, 3);
The problem is that func(*get_arg<param_types>(id)...); doesn't actually compile since param_types is a tuple and not a parameter pack. The compiler generates this error: "there are no parameter packs available to expand". What I would liked to have happened is for that line to expand to:
func(*get_arg<int>(id), *get_arg<char>(id));
And to have that work for any number of arguments. Is there any way to get that result?
This question seems similar but does not solve my problem by itself: "unpacking" a tuple to call a matching function pointer. I have a type list and from that I want to generate a list of values to use as function arguments. If I had the list of values I could expand them and call the function as outlined in that question, but I do not.
Not sure that is what do you want.
I don't know how to expand, inside call_func(), the parameters pack of params_type but, if you afford the use of a helper struct and a compiler with C++14...
I've prepared the following example with support for return type.
#include <tuple>
template<typename F>
struct function_traits;
template<typename T, typename R, typename... Args>
struct function_traits<R(T::*)(Args...) const> {
using return_type = R;
using param_types = std::tuple<Args...>;
};
template<typename T> struct function_traits : public
function_traits<decltype(&T::operator())> {};
template <typename T, typename ... Args>
T get_arg (std::tuple<Args...> const & tpl)
{ return std::get<typename std::decay<T>::type>(tpl); }
template <typename ...>
struct call_func_helper;
template <typename Func, typename Ret, typename ... Args>
struct call_func_helper<Func, Ret, std::tuple<Args...>>
{
template <typename T, typename R = Ret>
static typename std::enable_if<false == std::is_same<void, R>::value, R>::type
fn (Func const & func, T const & t)
{ return func(get_arg<Args>(t)...); }
template <typename T, typename R = Ret>
static typename std::enable_if<true == std::is_same<void, R>::value, R>::type
fn (Func const & func, T const & t)
{ func(get_arg<Args>(t)...); }
};
template <typename Func,
typename T,
typename R = typename function_traits<Func>::return_type>
R call_func (Func const & func, T const & id)
{
using param_types = typename function_traits<Func>::param_types;
return call_func_helper<Func, R, param_types>::fn(func, id);
}
int main()
{
call_func([](int const & a, char const & b) { }, std::make_tuple(3, '6'));
return 0;
}
Hope this helps.
How can I get a boolean value indicating if a known method has the const qualifier or not?
For example:
struct A {
void method() const {}
};
struct B {
void method() {}
};
bool testA = method_is_const<A::method>::value; // Should be true
bool testB = method_is_const<B::method>::value; // Should be false
In the type_traits header I found an is_const test I could use, but I need the method type, and I'm unsure how to obtain that.
I tried: std::is_const<decltype(&A::method)>::value but it doesn't work, and I can understand why (void (*ptr)() const) != const void (*ptr)()).
It is a lot simpler to check whether a member function can be called on a const-qualified lvalue.
template<class T>
using const_lvalue_callable_foo_t = decltype(std::declval<const T&>().foo());
template<class T>
using has_const_lvalue_callable_foo = std::experimental::is_detected<const_lvalue_callable_foo_t, T>;
Rinse and repeat, except with std::declval<const T>(), to check if said function can be called on a const-qualified rvalue. I can think of no good use cases for const && member functions, so whether there's a point in detecting this case is questionable.
Consult the current Library Fundamentals 2 TS working draft on how to implement is_detected.
It is a lot more convoluted to check whether a particular pointer-to-member-function type points to a function type with a particular cv-qualifier-seq. That requires 6 partial specializations per cv-qualifier-seq (const and const volatile are different cv-qualifier-seqs), and still can't handle overloaded member functions or member function templates. Sketching the idea:
template<class T>
struct is_pointer_to_const_member_function : std::false_type {};
template<class R, class T, class... Args>
struct is_pointer_to_const_member_function<R (T::*)(Args...) const> : std::true_type {};
template<class R, class T, class... Args>
struct is_pointer_to_const_member_function<R (T::*)(Args...) const &> : std::true_type {};
template<class R, class T, class... Args>
struct is_pointer_to_const_member_function<R (T::*)(Args...) const &&> : std::true_type {};
template<class R, class T, class... Args>
struct is_pointer_to_const_member_function<R (T::*)(Args..., ...) const> : std::true_type {};
template<class R, class T, class... Args>
struct is_pointer_to_const_member_function<R (T::*)(Args..., ...) const &> : std::true_type {};
template<class R, class T, class... Args>
struct is_pointer_to_const_member_function<R (T::*)(Args..., ...) const &&> : std::true_type {};
If you want const volatile to be true too, stamp out another 6 partial specializations along these lines.
The reason std::is_const<decltype(&A::method)>::value doesn't work is that a const member function isn't a const (member function). It's not a top-level const in the way that it would be for const int vs int.
What we can do instead is a type trait using void_t that tests whether we can call method on a const T:
template <typename... >
using void_t = void;
template <typename T, typename = void>
struct is_const_callable_method : std::false_type { };
template <typename T>
struct is_const_callable_method<T, void_t<
decltype(std::declval<const T&>().method())
> > : std::true_type { };
Demo
In C++20, things get a lot easier because concepts have been standardized, which subsumes the detection idiom.
Now all we need to write is this constraint:
template<class T>
concept ConstCallableMethod = requires(const T& _instance) {
{ _instance.method() }
};
ConstCallableMethod tests that the expression _instance.has_method() is well formed given that _instance is a const-reference type.
Given your two classes:
struct A {
void method() const { }
};
struct B {
void method() { }
};
The constraint will be true for A (ConstCallableMethod<A>) and false for B.
If you wish to also test that the return type of the method function is void, you can add ->void to the constraint like so:
template<class T>
concept ConstCallableMethodReturnsVoid = requires(const T& _instance) {
{ _instance.method() } -> void
};
If you wish to be a little more generic, you can pass in a member function pointer to the concept and test if that function pointer can be called with a const instance (although this gets a little less useful when you have overloads):
template<class T, class MemberF>
concept ConstCallableMemberReturnsVoid = requires(const T& _instance, MemberF _member_function) {
{ (_instance.*_member_function)() } -> void
};
You'd call it like so:
ConstCallableMemberReturnsVoid<A, decltype(&A::method)>
This allows for some other theoretical class like C, that has a const method, but it's not named method:
struct C
{
void foobar() const{}
};
And we can use the same concept to test:
ConstCallableMemberReturnsVoid<C, decltype(&C::foobar)>
Live Demo
Create a type trait to determine the const-ness of a method:
template<typename method_t>
struct is_const_method;
template<typename CClass, typename ReturnType, typename ...ArgType>
struct is_const_method< ReturnType (CClass::*)(ArgType...)>{
static constexpr bool value = false;
};
template<typename CClass, typename ReturnType, typename ...ArgType>
struct is_const_method< ReturnType (CClass::*)(ArgType) const>{
static constexpr bool value = true;
};