小编Vic*_*nko的帖子

已弃用的 get_io_service() 的替代方法

Asio v 1.11。该文档说basic_stream_socket::get_io_service()成员函数已被弃用,get_executor()必须改用。但后者executor不返回io_service

如何获取对io_servicesocket使用的对象的引用来构造另一个?

boost-asio

6
推荐指数
1
解决办法
5088
查看次数

std::any_cast() 和 std::get_if(std::variant) 是否绝对有必要将指针作为参数?

对于std::anystd::variant我们拥有的功能,要求有关当前包含值的对象,这回nullptr如果请求不能成立时(就像dynamic_cast做):

template<class ValueType>
const ValueType *any_cast(const any *operand);

template<class ValueType>
ValueType *any_cast(any *operand);
Run Code Online (Sandbox Code Playgroud)

template <class T, class... Types>
std::add_pointer_t<T> get_if(variant<Types...> *pv);

template <class T, class... Types>
std::add_pointer_t<const T> get_if(const variant<Types...> *pv);
Run Code Online (Sandbox Code Playgroud)

两者都将指针作为参数。为什么?它效率不高。实现每次都检查参数是否不是nullptr。请问nullptr参数使得任何意义可言?

此函数可以是类成员或将引用作为参数(名称可能略有不同)。这种次优设计的原因是什么?只是模拟dynamic_cast界面?

c++ c++17

5
推荐指数
1
解决办法
952
查看次数

返回时隐式移动std :: optional中包含的值

从C++ 11开始,我们就有了移动语义.在下面的示例中,将使用move-constructor(或复制elision),而不是像C++ 98中那样使用copy-constructor,而无需任何额外的工作.

std::string f()
{
    std::string res;
    ...
    return res; // <- move is used here instead of copy
}
Run Code Online (Sandbox Code Playgroud)

但是这个案子怎么样?

std::string f()
{
    std::optional<std::string> res;
    ...
    return *res; // <-- will the std::string value be moved??
}
Run Code Online (Sandbox Code Playgroud)

或者一个人必须写这样的东西?

std::string f()
{
    std::optional<std::string> res;
    ...
    return *std::move(res);
}
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics c++17

1
推荐指数
1
解决办法
466
查看次数

标签 统计

c++ ×2

c++17 ×2

boost-asio ×1

move-semantics ×1