AnswerBun.com

variadic template, Range, C++

Consider I have a struct.

template<typename T>
struct Range{
    T min, max;
    
    Range() = default;
    Range(T min = std::numeric_limits<T>::min(),T max = std::numeric_limits::max()) :
                                   min(min), max(max){
    } 
};

So this struct holds a range for any type T, and if not given it the range becomes the numeric limits of that type.
Note: Of course I check for T to be fundamental type but this question is not about that.

Now in my use case I want to have a struct like this

template<typename T,Range<T>...ranges>
struct Something{
    static std::array<Range<T>,sizeof...(ranges)> RANGES = {ranges...};
};

But when I want to "define" that something with this
using Defined = Something<int,Range<int>(100,200),Range<int>(200,300)>
I get compile-time error, because Range<T> is non-type template parameter and I am not using C++ 20. So I went further and updated my range struct like this.

template<typename T, T MIN = std::numeric_limits<T>::min(), T MAX = std::numeric_limits<T>::max()>
struct Range {
    T min = MIN, max = MAX;

    Range(T min, T max) : min(min), max(max) {}

    Range() = default;
};

And my Something struct’s template becomes this typename<T,typename... Args> so now using Defined = Something<int,Range<int,100,200>,Range<int,200,300>> works. So at this point I’ve got 2 questions, how can I statically assert for each of args to be a Range<T,T min, T max> . The second questions is about storing that. Look, I know that I have to store ...Args in a tuple, but if you look at my implementation of Range, can I somehow wrap them in an std::array<Range<T>,sizeof(args)>?

Stack Overflow Asked by Hrant Nurijanyan on January 28, 2021

1 Answers

One Answer

You can create trait is_range:

template <typename> struct is_range : std::false_type{};

template<typename T, T MIN, T MAX>
struct is_range<Range<T, MIN, MAX> : std::true_type{};

and then fold expression (C++17)

template <typename ...Ts>
struct Something{
    static_assert((is_range<Ts>::value && ...));
    static std::tuple<Ts...> RANGES;
};

Correct answer by Jarod42 on January 28, 2021

Add your own answers!

Related Questions

Recording using sox in c/c++

1  Asked on February 18, 2021

       

nodejs writing to JSON into a specific path

6  Asked on February 18, 2021 by c4llm3p3t3r

     

Unusual growth tempdb

0  Asked on February 18, 2021 by pablo-sanchez

       

Delphi Parse JSON

2  Asked on February 17, 2021 by stanley-mladenek

   

How to create a filter using bootstrap and jQuery

2  Asked on February 17, 2021 by jefin-winston

     

Change Canvas sizes with zoom

0  Asked on February 17, 2021 by reit-abdullah-yavuzkol

 

Is there a way to make the Loop Code make it faster?

4  Asked on February 17, 2021 by levesque-xylia

 

Using libcurl in g++

0  Asked on February 16, 2021 by marcel-kopera

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.