反构函数

问题描述 投票:0回答:1

如果这个问题以前有人问过,我很抱歉。我是个新手。

所以,我有以下代码。

template<typename K, typename V>

class test_class {
    std::map<K, V> my_map;

    void add_kv_to_std_map ( K const& key, V const& val ) {

        // And basically, i have the following syntax;
        auto [it, ins] = my_map.insert_or_assign(key, val);

        // Then perform other operations.

    }
}

问题是,在这个语法中,

auto [it, ins] = my_map.insert_or_assign(key, val);

我真的不需要 ins 变量。是否可以只检索 it 在那里?

起初,我以为我可以做这样的事情。

auto [it, ] = my_map.insert_or_assign(key, val);

但我产卵撒旦与此。

任何建议都是感激的。先谢谢你了。

c++ std
1个回答
2
投票

我真的不需要ins变量。是否可以直接在里面检索?

不可以。

你可以用一个变量名来表达它被忽略的意思。我个人更喜欢用下划线(注意下划线是全局命名空间中的保留标识符,所以不要在那里使用它)。

你可以使用 [[maybe_unused]] 属性来向编译器表明该绑定是有意未使用的。

[[maybe_unused]] auto [it, _] = ...

如果你使用 std::tie 而不是结构化绑定,那么您可以使用 std::ignore 对于这种情况。

std::map<K, V>::iterator it;
std::tie(it, std::ignore) = ...
© www.soinside.com 2019 - 2024. All rights reserved.