【问题标题】:Is it possible to get the index of a variant as a constexpr variable?是否可以将变体的索引作为 constexpr 变量获取?
【发布时间】:2024-04-29 14:15:03
【问题描述】:

我有以下代码 (play with example)。它检查variant 的底层价值是什么,并使用get 接收这个底层价值。如您所见,代码变得非常重复。

#include <variant>
#include <string>
#include <iostream>
#include <vector>

int main()
{
    std::variant<int, double, std::string> v;
    // v holds a string
    v = "hi there!";

    switch(v.index()) {
        case 0: 
            {   // brackets to avoid cross initialization
                int i = std::get<int>(v);
                // handle int
            }    
            break;
        case 1: 
            {
                double d = std::get<double>(v);
                // handle double
            }
            break;
        case 2: 
            {
                std::string s = std::get<std::string>(v);
                // handle string
            }
            break;
    }
}

问题:有没有办法将变体的索引作为constexpr 变量获取?我想做这样的事情:

// what I would like to do
// auto val_of_v = std::get<v.index()>(v);

// Error: template argument deduction/substitution failed
//        in 'constexpr' expansion of v<...>::index()

相关帖子: Get index by type in std::variant.

【问题讨论】:

  • 如果变量不是 constexpr,那么它所持有的类型在编译时是无法知道的。

标签: c++ switch-statement c++17 constexpr variant


【解决方案1】:

仅当变体是 constexpr 本身时:

constexpr std::variant<int, double> v{24};
auto a = std::get<v.index()>(v);

否则信息只能在运行时知道。

【讨论】:

    【解决方案2】:

    您正在寻找的是std::visit,它是为您实现该开关的库工具:

    std::variant<int, double, std::string> v;
    // v holds a string
    v = "hi there!";
    
    std::visit([](auto&& x){
        // x is whatever the variant was holding (could be int, double, or string)
    }, v);
    

    【讨论】:

      最近更新 更多