Say I have a class:
template<typename... Types>
class Example
{
public:
using types = std::tuple<Types...>;
template<size_t N> using dim_type = std::tuple_element_t<N, types>;
};
And I want to implement a member function that depends on the tuple element as follows, with the goal of having access to the number of arguments during compilation:
template<size_t N>
inline constexpr void do_stuff(const dim_type<N>& elems...)
{
constexpr size_t size_stuff = sizeof...(elems); // <-- this is the end goal
};
Problem is that this implementation of do_stuff()
won't do. And ultimately I would like the function to work like this:
Example<long,std::string> ex;
ex.do_stuff<0>(3);
ex.do_stuff<0>(5,6);
ex.do_stuff<1>("a","b","c","d");
ex.do_stuff<0>("a","b","c","d"); // <-- this line should not compile
ex.do_stuff<1>(1,"b"); // <-- nor should this one
And inside do_stuff
I should know at compile-time how many arguments are passed.
One possible answer, but I'm having an issue with it:
I figured that a proper implementation of this would require the use of variadic templates, however, I am having a problem getting the std::enable_if
part to work:
template<size_t N, typename... Args,
std::enable_if_t<static_and<std::is_convertible_v<Args, dim_type<N>>...>::value>* = nullptr>
inline constexpr void do_stuff(const Args&... elems)
{
constexpr size_t size_stuff = sizeof...(elems); // <-- this is the end goal
};
where static_and
is:
template<bool Head, bool... Tail>
struct static_and {
static constexpr bool value = Head && static_and<Tail...>::value;
};
template<bool Bool> struct static_and<Bool> {
static constexpr bool value = Bool;
};
It seems to me that the right way (a possible right way) is the one based on static_and
: a variadic type list Args
of argument that is SFINAE checked to be convertible to the right type.
I propose the following version of static_and
template <bool ...>
struct static_and : public std::false_type
{ };
template <>
struct static_and<> : public std::true_type
{ };
template <bool ... Bs>
struct static_and<true, Bs...> : public static_and<Bs...>
{ };
and do_stuff()
become
template <std::size_t I, typename ... Ts>
inline constexpr auto do_stuff (Ts const & ... elems)
-> std::enable_if_t<
static_and<std::is_convertible<Ts, type_n<I>>::value...>::value>
{
// now the number of elems is sizeof...(elems)
// or sizeof...(Ts)
std::cout << sizeof...(Ts) << std::endl;
}
The following is a full compiling example (with compilation errors when appropriate)
#include <tuple>
#include <iostream>
#include <type_traits>
template <bool ...>
struct static_and : public std::false_type
{ };
template <>
struct static_and<> : public std::true_type
{ };
template <bool ... Bs>
struct static_and<true, Bs...> : public static_and<Bs...>
{ };
template <typename ... Types>
struct foo
{
using types = std::tuple<Types...>;
static constexpr std::size_t num_types { sizeof...(Types) };
template <std::size_t I>
using type_n = std::tuple_element_t<I, types>;
template <std::size_t I, typename ... Ts>
inline constexpr auto do_stuff (Ts const & ... elems)
-> std::enable_if_t<
static_and<std::is_convertible<Ts, type_n<I>>::value...>::value>
{
// now the number of elems is sizeof...(elems)
// or sizeof...(Ts)
std::cout << sizeof...(Ts) << std::endl;
}
};
int main ()
{
foo<long, std::string> ex;
ex.do_stuff<0>(3); // compile; print 1
ex.do_stuff<0>(5, 6); // compile; print 2
ex.do_stuff<1>("a", "b", "c", "d"); // compile; print 4
// ex.do_stuff<0>("a", "b", "c", "d"); // compilation error
// ex.do_stuff<1>(1, "b"); // compilation error
}
If you can use C++17, instead of static_and
you can simply use template folding
template <std::size_t I, typename ... Ts>
inline constexpr auto do_stuff (Ts const & ... elems)
-> std::enable_if_t<(... && std::is_convertible<Ts, type_n<I>>::value)>
{
// now the number of elems is sizeof...(elems)
// or sizeof...(Ts)
std::cout << sizeof...(Ts) << std::endl;
}
If, in C++14, you prefer a constexpr
static_and()
function instead of a struct
, you can write it as follows
template <bool ... Bs>
constexpr bool static_and ()
{
using unused = bool[];
bool ret { true };
(void) unused { true, ret &= Bs... };
return ret;
}
So do_stuff()
become
template <std::size_t I, typename ... Ts>
inline constexpr auto do_stuff (Ts const & ... elems)
-> std::enable_if_t<
static_and<std::is_convertible<Ts, type_n<I>>::value...>()>
{
// now the number of elems is sizeof...(elems)
// or sizeof...(Ts)
std::cout << sizeof...(Ts) << std::endl;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With