std::optional<T> call by reference

旧街凉风 提交于 2021-01-29 07:11:36

问题


I am implementing the following function:

bool DenStream::_try_merge(const double sample,
                           const double weight,
                           const std::optional<MicroCluster>& micro_cluster) const {
    if (micro_cluster) {
        MicroCluster micro_cluster_copy(*micro_cluster);
        micro_cluster_copy.insert_sample(sample, weight);
        if (micro_cluster_copy.get_radius() <= this->eps) {
            micro_cluster.insert_sample(sample, weight); // THIS DOES NOT WORK
            return true;
        }
    }
    return false;
};

The compiler says error: 'const class std::optional' has no member named 'insert_sample'.

I understand the error, but I have not been able to find a solution that works. If I write

*micro_cluster.insert_sample(sample, weight);

the compiler says error: 'const class std::optional' has no member named 'insert_sample'. I have also tried with emplace() and value().

I would appreciate if anyone can help me.

Okay, now it works. As suggested in the comments, I changed to

micro_cluster->insert_sample(sample, weight);

and the interface has to be

bool DenStream::_try_merge(const double sample,
                           const double weight,
                           std::optional<MicroCluster>& micro_cluster)

Thanks!

来源:https://stackoverflow.com/questions/61096479/stdoptionalt-call-by-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!