首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::variant::index

constexpr std::size_t index() const

?

(since C++17)

返回当前由变体保存的替代方案的基于零的索引。

如果变体是无价值[医]通过[医]例外、回报变体[医]非营利组织...

例外

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#include <variant>
#include <string>
#include <iostream>
int main()
{
    std::variant<int, std::string> v = "abc";
 
    std::cout << "v.index = " << v.index() << '\n';
 
    v = {}; 
 
    std::cout << "v.index = " << v.index() << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
v.index = 1
v.index = 0

二次

另见

holds_alternative (C++17)

checks if a given type appears exactly once in a variant (function template)

std::get(std::variant) (C++17)

reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com