I need to create a template class depending on a simple struct like this. It has a method AddNew to insert new element for its tuple member.
struct Type
{
int i;
char c;
};
template<typename ...T>
class A
{
std::tuple<T...> ts;
public:
A(T&&...t):ts(std::move(t)...){}
void AddNew(T t)
{
ts=std::tuple_cat(ts,t);
}
};
int main()
{
A<Type,Type,Type,Type> a({1,'a'},{2,'t'},{3,'c'},{4,'g'});
Type t{5,'x'};
a.AddNew(t);
}
Problem I don't want to write down Type... 4 times like in main. I like something like A<Type,4> when I initialize A with 4 Types or A<Type, 100> for 100 Types.
You probably just want vector:
struct Type
{
int i;
char c;
};
template<typename T>
class A
{
std::vector<T> data;
public:
template <typename ... Ts>
A(Ts&&...ts) : data(std::forward<Ts>(ts)...){}
void AddNew(const T& t)
{
data.push_back(t);
}
};
int main()
{
A<Type> a(Type{1,'a'}, Type{2,'t'}, Type{3,'c'}, Type{4,'g'});
Type t{5,'x'};
a.AddNew(t);
}
you may partially specialize A for some helper 'tag' type:
template<typename T, std::size_t N>
struct repeat{};
template<typename... T>
struct repeat_unpack{ using type = A<T...>; };
template<typename T, int N, typename... V >
struct repeat_unpack<repeat<T,N>,V...>: std::conditional_t<(N>1),
repeat_unpack<repeat<T,N-1>,T,V...>,
repeat_unpack<T,V...>
>{};
template<typename T, int N>
class A<repeat<T,N>>: repeat_unpack<repeat<T,N>>::type {};
// to be used as
A<repeat<Type,3>>
the above uses inheritance to avoid code duplication, so you'll probably need to lift some members (eg. constructors) to the partial specialization as well.
Otherwise, just use an alias
template<class T, std::size_t N>
using A_nth = typename repeat_unpack<repeat<T,N>>::type;
A_nth<Type,3>
Just one more thing, given that you're using tuple internally, it should be easy to simply specialize for std::array and use it instead, being tuple compatible ...
PS. obviously, your AddNew() code makes no sense as is, I'd assume it's just a code snippet typo ...
Related
I am new to C++20. The intention here is to have a template class which has value whose type would be the underlying type of T that's passed in.
So in case of T being:
std::optional<char>, it's char value
int, it's just int value.
Is there any better way to extract the types than through struct TypeExtract? More or a generic solution in C++20 perhaps? Given if the class could take more than just std::optional<int> or just a primitive type?
Can the condition in foo be improved specially with the way val is initialized?
template<typename T>
constexpr bool is_optional = false;
template<typename T>
constexpr bool is_optional<std::optional<T>> = true;
template<typename T>
struct TypeExtract
{
using type = T;
};
template<typename T>
struct TypeExtract<std::optional<T>>
{
using type = T;
};
template <typename T>
concept is_integral = std::is_integral_v<typename TypeExtract<T>::type>;
template <is_integral T>
class A
{
using Type = typename TypeExtract<T>::type;
Type val;
void foo(T value)
{
if constexpr (is_optional<T>)
{
val = *value;
}
else
{
val = value;
}
}
};
int main()
{
A<char> a1;
A<std::optional<int>> a2;
// A<double> a3; // fails
}
It looks like you're trying to extract first template parameter from a class template and keep on unwinding templates until you get to a non-template type. In that case you could make a type trait that is specialized for types instantiated from templates:
// primary template
template<class T, class...>
struct type {
using value_type = T;
};
// specialization for template instantiated types
template<template<class, class...> class T, class F, class... Rest>
struct type<T<F, Rest...>> {
using value_type = typename type<F>::value_type;
};
// helper alias
template<class... Ts>
using type_t = typename type<Ts...>::value_type;
You could then use it like so:
int main() {
type_t<char> a1;
type_t<std::optional<int>> a2;
type_t<double, int> a3;
static_assert(std::is_same_v<decltype(a1), char>);
static_assert(std::is_same_v<decltype(a2), int>);
static_assert(std::is_same_v<decltype(a3), double>);
}
There is no good or bad here, it's a matter of style and convention, but personally I would get rid of if constexpr and take advantage of trailing requires for the sake of reducing function's cyclomatic complexity. On the other hand, that add some boilerplate. The choice is yours.
Not much can be done about type extraction, though I would probably use a templated base and import its member(s) instead of importing the type into the class. Not a big deal, but it feels more idiomatic to me.
As for concepts, I'd probably use more library-provided ones in the place of type traits.
Side note: consider using assert, .value() or similar function when assigning from the optional to ensure it's not empty.
All in all, I'd probably write your code somewhat this way:
#include <concepts>
#include <type_traits>
#include <optional>
template<typename T>
concept StdOptional = std::same_as<std::optional<typename T::value_type>, T>;
template<typename T>
concept OptionalIntegral = StdOptional<T> and std::integral<typename T::value_type>;
template<typename T>
concept OptionalOrOptionalIntegral = std::integral<T> or OptionalIntegral<T>;
template<typename>
struct ABase;
template<std::integral T>
struct ABase<T>
{
T value;
};
template<OptionalIntegral T>
struct ABase<T>
{
typename T::value_type value;
};
template<OptionalOrOptionalIntegral T>
class A : ABase<T>
{
using ABase<T>::value;
public:
void setValue(T val) requires(std::integral<T>)
{
value = val;
}
void setValue(T val) requires(OptionalIntegral<T>)
{
value = val.value();
}
};
Demo: https://godbolt.org/z/dzvr9xbGr
I fully expect this to not be a feature, but figured I may as well ask; is it possible to expand code at compile time using template parameters?
For example:
template <size I>
void foo()
{
...double... vec;
}
Where the ... Is replaced by std::vector< >, I times.
So foo<2>() would compile to:
void foo()
{
std::vector<std::vector<double>> vec;
}
I can't even imagine what the syntax for this would be, so I'm not hopeful.
It would be useful for something like an N dimensional binning class, which could also be implemented through recursion, but not so neatly imo.
Yes, you can. You can do it with class templates and specializations, like this:
template<std::size_t N>
struct MultiDim {
using underlying = typename MultiDim<N-1>::type;
using type = std::vector<underlying>;
};
template<>
struct MultiDim<1> {
using type = std::vector<double>;
};
template<std::size_t N>
using multi_dimensional = typename MultiDim<N>::type;
Hence, multi_dimensional<1> is vector<double>, and multi_dimensional<N> where N>1 is vector<multi_dimensional<N-1>>.
is it possible to expand code at compile time using template parameters?
Directly... I don't think so.
But I suppose you can implement a specific type traits as follows
template <typename T, std::size_t I>
struct bar
{ using type = std::vector<typename bar<T, I-1U>::type>; };
template <typename T>
struct bar<T, 0U>
{ using type = T; };
and use in foo() in this way
template <std::size_t I>
void foo ()
{
typename bar<double, I>::type vec;
}
If you want to be a little more generic, you can also pass std::vector as a template-template parameter; if you define bar as follows
template <template <typename...> class C, typename T, std::size_t I>
struct bar
{ using type = C<typename bar<C, T, I-1U>::type>; };
template <template <typename ...> class C, typename T>
struct bar<C, T, 0U>
{ using type = T; };
foo() become
template <std::size_t I>
void foo ()
{
typename bar<std::vector, double, I>::type vec;
}
I want to implement a class template that:
behaves like a function
it's input and output variables are all shared.
relatively easy to use.
As a result, I construct the following:
// all input/output variable's base class
class basic_logic_parameter;
// input/output variable, has theire value and iterators to functions that reference to this variable
template <typename FuncIterator, typename ValueType>
class logic_parameter
:public basic_logic_parameter
{
private:
std::list<FuncIterator> _refedFuncs;
ValueType _val;
public:
};
// all `function`'s base class
class basic_logic_function
{
public:
virtual ~basic_logic_function() = 0;
};
// the function, has input/output variable
template <typename FuncIterator, typename R, typename... Args>
class logic_function_base
:public basic_logic_function
{
private:
std::shared_ptr<logic_parameter<FuncIterator, R>> _ret;
std::tuple<std::shared_ptr<logic_parameter<FuncIterator, Args>>...> _args;
public:
template <std::size_t N>
decltype(auto) arg()
{
return std::get<N>(_args);
}
template <std::size_t N>
struct arg_type
{
typedef std::tuple_element_t<N> type;
};
template <std::size_t N>
using arg_type_t = arg_type<N>::type;
decltype(auto) ret()
{
return _ret;
}
};
I wish to use as these like:
// drawing need color and a pen
struct Color
{
};
struct Pen
{
};
struct Iter
{
};
class Drawer
:public logic_function_base<Iter, void(Color, Pen)>
{
public:
void draw()
{
arg_type_t<0> pColor; // wrong
}
}
My compiler can not pass this code through, why? I just want convert a template parameter pack to std::tuple of std::shared_ptr of them.
for example:
Given struct A, int, struct C, I want to have:
std::tuple<
std::shared_ptr<logic_parameter<A>>,
std::shared_ptr<logic_parameter<int>>,
std::shared_ptr<logic_parameter<C>>,
>
The problem (once the small errors are fixed1) is that you instantiate:
logic_function_base<Iter, void(Color, Pen)>
...meaning that FuncIterator is Iter and R is void(Color, Pen), so Args is emtpy <>, so decltype(_args) is an empty std::tuple<>, and your code fails to obtain the type of the 0th element of an empty tuple, which is legit.
What you want is partial specialization of logic_function_base:
template <typename F, typename T>
class logic_function_base;
template <typename FuncIterator, typename R, typename... Args>
class logic_function_base<FuncIterator, R(Args...)>: public basic_logic_function {
};
1 Small mistakes in your current code:
template <std::size_t N>
struct arg_type
{
typedef std::tuple_element_t<N, decltype(_args)> type; // Missing the tuple type
};
template <std::size_t N>
using arg_type_t = typename arg_type<N>::type; // Missing a typename
This may not answer your whole question, but you could use the following trait to wrap tuple element types.
template <typename T> struct wrap;
template <typename... T>
struct wrap<std::tuple<T...>> {
using type = std::tuple<std::shared_ptr<logic_parameter<T>>...>;
}
template <typename T>
using wrap_t = typename wrap<T>::type;
You can then use it like this:
std::tuple<int,double,char> t1;
wrap_t<decltype(t)> t2;
The type of t2 is std::tuple<std::shared_ptr<logic_parameter<int>>,std::shared_ptr<logic_parameter<double>>,std::shared_ptr<logic_parameter<char>>>.
I need to declare a class which could store different kind of containers. i.e. It would be nice if it could handle std::bitset and std::array.
However, these two classes need a different of template arguments...
Is it possible (and possibly, how) to use templated template classes and variadic templates to declare this kind of class?
Example (but wrong):
template<template <typename..., std::size_t> class Container,
std::size_t N,
typename... Args>
class Base_Class
{
...
Container<Args..., N/2> container;
};
The compiler complains that N/2 is not a type. Obviously, for both std::array and std::bitset I need the size to be the last template parameter... Is it possible to code this crazyness?
Thank you!
EDIT:
As far as I am concerned, the main problem is that variadic templates can only be expanded on the right, therefore the variadic parameter must be the last one. Anyone know if there are any plans to allow the following syntax in C++17?
template<typename... Args, typename T>
struct A
{};
Anton's answer can be made somewhat less container-specific by using template template parameters for the speciliasations of ResizedContainer:
namespace detail {
template<typename Container>
struct ResizedContainer;
template<template<typename,std::size_t> class Container,
typename T, std::size_t N>
struct ResizedContainer<Container<T,N>> {
using type = Container<T,N/2>;
};
template<template<std::size_t> class Container,
std::size_t N>
struct ResizedContainer<Container<N>> {
using type = Container<N/2>;
};
}
#include <array>
#include <bitset>
template<typename Container>
class Base_Class {
typename detail::ResizedContainer<Container>::type container;
};
int main() {
Base_Class<std::array<int,4>> a;
Base_Class<std::bitset<5>> b;
}
Maybe something like this:
namespace detail {
template<typename Container>
struct ResizedContainer;
template<typename T, size_t N>
struct ResizedContainer<std::array<T, N>> {
using type = std::array<T, N/2>;
};
template<size_t N>
struct ResizedContainer<std::bitset<N>> {
using type = std::bitset<N/2>;
};
}
template<typename Container>
class Base_Class {
typename detail::ResizedContainer<Container>::type container;
};
int main() {
Base_Class<std::array<int, 4>> a;
Base_Class<std::bitset<5>> b;
}
I know that I can expand a parameter pack of tuples onto a variadic template of base classes
like this:
#include <tuple>
struct ComponentA {
int foo;
int bar;
ComponentA(std::tuple<int, int> && pack):
foo(std::get<0>(pack)),
bar(std::get<1>(pack))
{}
};
struct ComponentB {
ComponentB(std::tuple<> && pack)
{}
};
template<typename ... Bases>
struct CompositeClass: public Bases... {
template<typename ... Arguments>
CompositeClass(Arguments &&... arguments):
Bases(std::forward<Arguments>(arguments))...
{}
};
int main() {
CompositeClass<ComponentA, ComponentB> composite{
std::make_tuple(100, 100),
std::make_tuple()
};
}
However, I find that syntax cumbersome. Is there a way to avoid the tuples altogether?
Edit:
Something more like this:
struct ComponentA {
int foo;
int bar;
ComponentA(int a, int b):
foo(a),
bar(b)
{}
};
struct ComponentB {
ComponentB()
{}
};
template<typename ... Bases>
struct CompositeClass: public Bases... {
template<typename ... ... Arguments>
CompositeClass(Arguments &&... ... arguments):
Bases(std::forward<Arguments>(arguments)...)...
{}
};
int main() {
CompositeClass<ComponentA, ComponentB> composite{100, 100};
}
Both parameters are passed to ComponentA, none to ComponentB.
Edit 2
So I have got something like this:
template <int ...>
struct SequenceContainer {};
template <int, typename>
struct AppendIntToSequence;
template <int Value, int ... Sequence>
struct AppendIntToSequence<Value, SequenceContainer<Sequence...>> {
typedef SequenceContainer<Sequence..., Value> type;
};
template<int End>
struct MakeSequence:
AppendIntToSequence<End - 1, typename MakeSequence<End - 1>::type> {};
template<>
struct MakeSequence<0> {
typedef SequenceContainer<> type;
};
struct ComponentA {
static const int parameters = 2;
ComponentA(int && a, int && b) {
std::cout << a << b << std::endl;
}
};
struct ComponentB {
static const int parameters = 1;
ComponentB(const char * string) {
std::cout << string << std::endl;
}
};
template <typename Base>
struct TupleConstructor: Base {
template <typename ... Arguments, int ... Sequence>
TupleConstructor(std::tuple<Arguments...> &&, SequenceContainer<Sequence...> const &);
};
template <typename Base>
template <typename ... Arguments, int... Sequence>
TupleConstructor<Base>::TupleConstructor(std::tuple<Arguments...> && arguments, SequenceContainer<Sequence...> const &):
Base(std::forward<Arguments>(std::get<Sequence>(arguments))...)
{}
template <typename ... Components>
struct CompositeClass: public TupleConstructor<Components>... {
template <typename ... Arguments>
CompositeClass(Arguments &&... arguments):
TupleConstructor<Components>(
std::forward<Arguments>(arguments),
typename MakeSequence<std::tuple_size<Arguments>::value>::type{}
)...
{}
};
int main()
{
CompositeClass<ComponentA, ComponentB> composite{
std::tuple<int &&, int &&>(100,100),
std::tuple<const char *>("Hello World!")
};
However, I haven't been able to figure out how to remove the two tuples from the CompositeClass constructor. How can this be done?
It seems, if you cut down a bit on sprinkling periods all over the place you should be OK! Parameter packs aren't anything particular special: they are just expanded into a comma separate list. That is, if you change your code to become that below you should be OK:
template <typename... Bases>
struct CompositeClass: public Bases... {
template <typename... Arguments>
CompositeClass(Arguments&&... arguments):
Bases(std::forward<Arguments>(arguments))...
{}
};
Except for adding a colon in front of the initializer list I have only removed some excess "...". This works OK as long as all your template arguments are actually class types and as long as they happen to be different. Obviously, types which aren't classes can't be used as bases. If You need the same type multiple times as base you need to inherit them indirectly why an auxiliary type which gives them a number. Generating the numbers is a bit tricky the first few times you do it but nothing really magically either.
Expanding on the edited question: You mean, you want to pass lists of arguments to each individual base class constructor? If it is OK to pass in std:tuple<...> as arguments to your CompositeClass this is doable, too. Essentially, you need to transform each std::tuple<...> into a list of arguments. This also requires the generation of said indices. I would at least start off with an auxiliary base which takes a std::tuple<...> as argument and forwards the members as arguments to the base. This would use something similar to the code above for the CompositeClass and the main trick would be in an auxiliary class:
template <int... Numbers> struct Sequence {};
template <int N> struct MakeSequence;
template <typename Base>
struct AuxiliaryBase: Base {
template <typename Tuple, int... Numbers>
AuxiliaryBase(Tuple&&, Sequence<Numbers...> const&);
};
template <typename... Bases>
struct CompositeClass: public AuxiliaryBase<Bases>... {
template <typename... Args>
CompositeClass(Args&&... arguments):
AuxiliaryBase<Bases>(std::forward<Args>(arguments),
typename MakeSequence<std::tuple_size<Args>::size>())...
{}
};
Implementing the constructor of AuxiliaryBase essentially requires that there is a facility creating a sequence of integers from 0 to std::tuple_size<Tuple>::value. This take a bit tweaking but is definitely doable. To make them available an auxiliary parameter is passed in (I'm not sure if this can be avoided; well, probably it can be packaged as a type with the std::tuple<...> if this should be avoided). With this, the base class constructor is fairly straight forward:
template <typename Base>
template <typename Tuple, int... Numbers>
AuxiliaryBase<Base>::AuxiliaryBase(Tuple&& tuple, Sequence<Numbers...> const&):
Base(std::get<Numbers>(tuple)...)
{}
I haven't tested it but something along these lines should actually work. What this version doesn't do is perfect forwarding of the members in the std::tuple<...>: for this you would need a variation of std::forward<>() which takes three arguments which uses the reference qualifications of the outer type to determine which kind of reference needs to be forward for the member. I haven't tried this but it is probably an interesting exercise as well.
I haven't tried to compile this particular example but I have definitely done something like this in the past: if you look the slides for the presentation I gave at the ACCU conference in 2010 you will find all the details on how to do this (including how to create a sequence of integers; a very short of this, actually).