trinalbadger587
trinalbadger587

Reputation: 2109

Is there a way of changing the type used in a struct depending on at template parameter in c++?

What I want is a way to have different data structures for the struct depending on template parameters

template<uint32_t width, uint32_t height>
struct S
{
    templateif (width * height) > 32:
        uint64_t val;
    templateelse:
        uint32_t val;
    templatend;
}

Something like what you see above. Is there any way of changing data types based on template parameters?

I thought something like this might work:

struct S
{
    static const bool bit64 = (width * height) > 32;
    template <typename T>
    struct Arr
    {
        T val;
    }
    using DataType = Arr<bit64 ? uint64_t : uint32_t>;
    Arr data;
}

but it didn't.

Upvotes: 0

Views: 152

Answers (2)

Coral Kashri
Coral Kashri

Reputation: 3506

#include <iostream>

// Define your conditions
template <int Fst, int Snd, typename = void>
struct is_bigger_than : std::false_type {};

template <int Fst, int Snd>
struct is_bigger_than <Fst, Snd, typename std::enable_if_t<(Fst > Snd), void>> : std::true_type {};

// Define short access ways to your conditions    
template <int Fst, int Snd>
constexpr bool is_bigger_than_v = is_bigger_than<Fst, Snd>::value;

// Define your base class
template <uint32_t width, uint32_t height, bool = is_bigger_than_v<width * height, 32>>
struct S {
    std::uint32_t val;

    S() {
        std::cout << "Smaller" << std::endl;
    }
};

// Partial specialized template class
template<uint32_t width, uint32_t height>
struct S<width, height, true> {
    uint64_t val;

    S() {
        std::cout << "Bigger" << std::endl;
    }
};

// Some tests
int main() {
    S<2,16> s1;
    S<2,17> s2;

    return EXIT_SUCCESS;
}

You can use more variations than true/false, if you have more than one condition case.

Edit

I've made some generalizations to the condition is_bigger_than in my new GitHub repository. Now it'll let you perform a lot more conditions (including custom condition using general_compare_v) in the same way:

template<int Fst, int Snd, typename Op, typename = void>
struct general_compare : std::false_type {};

template<int Fst, int Snd, typename Op>
struct general_compare<Fst, Snd, Op, typename std::enable_if_t<(Op()(Fst, Snd)), void>> : std::true_type {};

template<int Fst, int Snd, typename Op>
constexpr bool general_compare_v = general_compare<Fst, Snd, Op>::value;

// is_bigger_than

template<int Fst, int Snd>
constexpr bool is_bigger_than_v = general_compare_v<Fst, Snd, std::greater<>>;

template<int Fst, int Snd>
constexpr bool is_bigger_equal_v = general_compare_v<Fst, Snd, std::greater_equal<>>;

template<int Fst, int Snd>
constexpr bool is_lower_than_v = general_compare_v<Fst, Snd, std::less<>>;

// ... And some more ...

// Now you can define your classes in the same way I showed before:

// Define your base class
template <uint32_t width, uint32_t height, bool = is_bigger_than_v<width * height, 32>>
struct S {
    std::uint32_t val;
    // ...
};

// Partial specialized template class
template<uint32_t width, uint32_t height>
struct S<width, height, true> {
    uint64_t val;
    // ...
};

A link to the repository.

Upvotes: 0

Mestkon
Mestkon

Reputation: 4046

You can try std::conditional

template<std::uint32_t width, std::uint32_t height>
struct S
{
    using type = typename std::conditional<(width * height > 32), 
        std::uint64_t, std::uint32_t>::type;

    type val;
}

Upvotes: 3

Related Questions