#Using a variadic template constructor in header file

14 messages · Page 1 of 1 (latest)

stone kestrel
#

Hello,
I'm trying to write a little program with hpp files which contains variadic template constructor.
Also, I would like to not implement my constructor in my header file.
The "type" of my template will always be "MyWindowFlags" (an enum class), is there a way to solve my problem ?

ashen quiverBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

runic fulcrum
#

why not just exclude the template altogether if the type is always going to be the same?. just pass in a vector/initializer_list/range of the flags instead... or just go with the classic |'ing them together if they're just bit flags, then you only need 1 parameter and nothing needs to be variadic.

#

if the reason you're looking into templates is to get around the fact that enum class types don't allow for the bitwise| operator, you can always just define the operator for that type so you can simplify things:

constexpr MyWindowFlags operator|(const MyWindowFlags lhs, const MyWindowFlags rhs)
{
    return static_cast<MyWindowFlags>(std::to_underlying(lhs) | 
                                      std::to_underlying(rhs));
}
stone kestrel
#

I would like to pass only "unique" parameters, not a structure containing all the "MyWindowFlags" (I'm trying to create a small library which must be simple and intuitive). I found another solution, Explain the instantiations of the template in the file like that :

template MyWindow::MyWindow(int, int, MyWindowFlags);
template MyWindow::MyWindow(int, int, MyWindowFlags, MyWindowFlags);
template MyWindow::MyWindow(int, int, MyWindowFlags, MyWindowFlags, MyWindowFlags);

It's not very clean but it seems to work.

runic fulcrum
#

you wouldn't need a struct to contain the values, just a single variable, as long as each flag represents a unique bit.

#

;compile -std=c++23

#include <iostream>
#include <utility>

enum class MyWindowFlags {
    Flag1 = 1 << 0,
    Flag2 = 1 << 1,
    Flag3 = 1 << 2,
    Flag4 = 1 << 3,
    Flag5 = 1 << 4,
    Flag6 = 1 << 5,
    Flag7 = 1 << 6,
    Flag8 = 1 << 7,
};

constexpr MyWindowFlags operator|(const MyWindowFlags lhs, const MyWindowFlags rhs)
{
    return static_cast<MyWindowFlags>(std::to_underlying(lhs) |
                                      std::to_underlying(rhs));
}

constexpr bool operator&(const MyWindowFlags lhs, const MyWindowFlags rhs)
{
    return 0 != (std::to_underlying(lhs) & std::to_underlying(rhs));
}

int main(const int argc, char** argv)
{
    constexpr MyWindowFlags flags = MyWindowFlags::Flag1 | MyWindowFlags::Flag3 |
                                    MyWindowFlags::Flag4 | MyWindowFlags::Flag8;

    constexpr bool flag1_set = flags & MyWindowFlags::Flag1;
    constexpr bool flag2_set = flags & MyWindowFlags::Flag2;
    constexpr bool flag3_set = flags & MyWindowFlags::Flag3;
    constexpr bool flag4_set = flags & MyWindowFlags::Flag4;
    constexpr bool flag5_set = flags & MyWindowFlags::Flag5;
    constexpr bool flag6_set = flags & MyWindowFlags::Flag6;
    constexpr bool flag7_set = flags & MyWindowFlags::Flag7;
    constexpr bool flag8_set = flags & MyWindowFlags::Flag8;

    std::cout << "Flag1 set = " << (flag1_set ? "true" : "false") << '\n'
              << "Flag2 set = " << (flag2_set ? "true" : "false") << '\n'
              << "Flag3 set = " << (flag3_set ? "true" : "false") << '\n'
              << "Flag4 set = " << (flag4_set ? "true" : "false") << '\n'
              << "Flag5 set = " << (flag5_set ? "true" : "false") << '\n'
              << "Flag6 set = " << (flag6_set ? "true" : "false") << '\n'
              << "Flag7 set = " << (flag7_set ? "true" : "false") << '\n'
              << "Flag8 set = " << (flag8_set ? "true" : "false") << '\n';
}
plucky lakeBOT
#
Program Output
Flag1 set = true
Flag2 set = false
Flag3 set = true
Flag4 set = true
Flag5 set = false
Flag6 set = false
Flag7 set = false
Flag8 set = true
runic fulcrum
#

that snippet just shows you how you can stuff any number of flags into a single variable using |, then figure out which individual flags are set using &

#

that way the only parameter your class should need to take in would be a single MyWindowFlags variable in case you'd still be more interested in something like that. it's definitely very common for this type of thing

stone kestrel
#

I will do that, thank you very much for your help !

runic fulcrum
#

np

ashen quiverBOT
#

This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

stone kestrel
#

!solved