Home > front end >  How to compare data type of a tuple parameter?
How to compare data type of a tuple parameter?

Time:09-02

How i could compare the data type of each tuple argument?

enum WinGetCmds {
    WINGET_TITLE, WINGET_CLASS
};


template <typename... Args>
auto WinGet(WinGetCmds cmd, Args&&... args)
{
    auto pack = std::make_tuple(std::forward<Args>(args)...);

    switch (cmd)
    {
    case WINGET_TITLE:
    {
        auto arg_1 = std::get<0>(pack);

        if constexpr (std::is_same<arg_1, std::wstring>::value)
        {
          //...
        }
        else if constexpr (std::is_same<arg_1, HWND>::value)
        {
          //...
        }    

    }
    break;

    }
}


WinGet(WINGET_TITLE, L"ABC");

In the template above I'm getting an error in the constexpr line:

'std::is_same': 'arg_1' is not a valid template type argument for parameter '_Ty1'7

What other way I could compare the data type of arg_1?

CodePudding user response:

What other way I could compare the data type of arg_1?

The problem is that arg_1 is an expression when you use it in an expression. That is, you cannot use it as a template type parameter for is_same which is what the error is trying to say.

To solve this, you could use, decltype(arg_1).

Working demo

  • Related