小编RaG*_*__M的帖子

当服务器执行 asio::write 操作时客户端断开连接时,Boost TCP 服务器崩溃

为了测试我的服务器,我从客户端使用 for 循环创建了 100 个请求,当我的服务器正在为第 N 个请求写入响应时,我故意从客户端按了 control+c,就是这样。尽管我尝试使用新连接来连接服务器,但服务器停止并且没有响应,任何人都可以建议我如何使我的服务器稳定并免受此类中断的影响。这是我的服务器:

class tcp_server
 {
 public:

 tcp_server(boost::asio::io_service& io_service)
   : acceptor_(io_service, tcp::endpoint(tcp::v4(), 2020))
 {
    start_accept();
 }

 private:

 void start_accept()
 {
    tcp_connection::pointer new_connection =
        tcp_connection::create(acceptor_.get_io_service());

    acceptor_.async_accept(new_connection->socket(),
        boost::bind(&tcp_server::handle_accept, this, new_connection,
        boost::asio::placeholders::error));
 }

 void handle_user_read(const boost::system::error_code& err,
    std::size_t bytes_transferred)
 {
 }


 void handle_accept(tcp_connection::pointer new_connection,
    const boost::system::error_code& error)
 {
    if (!error)
    {
       new_connection->start();
       start_accept();
    }

 }
 tcp::acceptor acceptor_;
 };
Run Code Online (Sandbox Code Playgroud)

这是我的 TCP 连接:

class tcp_connection : public boost::enable_shared_from_this<tcp_connection>
 {
   public:

   typedef boost::shared_ptr<tcp_connection> pointer;

   static pointer create(boost::asio::io_service& io_service)
   {
      return pointer(new …
Run Code Online (Sandbox Code Playgroud)

c++ boost tcp blocking boost-asio

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

使用 std::tuple_size 时导致“错误:不完整类型”

我在命名空间中有一个类模板
并且我在该命名空间内有一个非成员函数,它返回std::tuplestd::shared_ptr(s)<myown::myclass<Type>>
调用该函数F()并将其 result( std::tuple) 传递给另一个非成员函数(在myown命名空间内)

namespace myown {

    template<typename T> class myclass{ /*...*/};

    template<>           class myclass<void>{ /*...*/};

    auto F()
    {
        return std::make_tuple(std::shared_ptr<myclass<int>>(),
                               std::shared_ptr<myclass<const char *>>(),
                               std::shared_ptr<myclass<int>>());
    }

    template <typename tup>
    auto anotherF(tup&& result){
        size_t s = std::tuple_size<tup>::value;//
        /*...*/
    }
}

 int main()
 {
     auto tr = myown::F();
     myown::anotherF(tr);
 }
Run Code Online (Sandbox Code Playgroud)

std::tuple_size<tup>结果的使用error: incomplete type...used in nested name specifier

c++ c++14

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

为什么rvalue不能分配给constexpr引用变量

我有以下代码

constexpr int into(int a,int b)
{
  int c=a*b;
  return c;
}

int main()
{
 constexpr int &n=into(5,5);

}
Run Code Online (Sandbox Code Playgroud)

我已阅读(在MSDN中)

该关键字constexpr是在C++ 11中引入的,并在C++ 14中进行了改进.这意味着不断表达.例如const,它可以应用于变量,以便在任何代码尝试修改该值时引发编译器错误.

在我阅读之后,我认为constexpr可以代替使用const,但对于上面的代码我得到一个编译器错误说明

`int main()':
invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'`
Run Code Online (Sandbox Code Playgroud)

什么时候constexpr更换const,它工作正常.我不明白这种行为; 有人可以解释一下吗?

c++ constexpr c++11 c++14

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

Big Oh是唯一用于衡量STL复杂性的符号

我已经开始阅读C++ STL并且还找到了一本书!当我正在阅读复杂性时,它在选择算法和数据结构方面发挥了重要作用我已经看到Big Oh表示法仅用于不同的变量(O(n),O(log(n).,))冲浪我发现大哦表示f(x) = O(g(x))--(big-oh) means that the growth rate of f(x) is asymptotically less than or equal to to the growth rate of g(x)

所以我的问题是,如果算法时间复杂度总是等于g(x)的增长,为什么我们提到复杂性f(x)=O(n)[Big oh of n]而不是使用(theta),因为当我读到(theta)时所说的f(x) = ?(g(x)) (theta) means that the growth rate of f(x) is asymptotically equal to the growth rate of g(x)

这里的符号可能是(θ)而不是O(N)不是吗?或任何使用大哦的原因.

我们应该使用什么样的符号来衡量空间的复杂性.在这本书中,我没有看到关于空间复杂性的任何关于STL的讨论.

参考:Θ(n)和O(n)之间有什么区别?

c++ algorithm stl time-complexity asymptotic-complexity

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

为什么字符串文字不是纯右值

这次我一直在研究值类别。我对所有关于类别的定义和解释都满意,但我觉得如果有人能解释一下为什么字符串文字不是一个值类别会更好prvale

prvalue我知道当表达式没有身份但可移动时,它的值/结果就属于类别

int i=42;char i='a'; //prvalue
string i ="notprvalue";
Run Code Online (Sandbox Code Playgroud)

难道字面上的“notprvalue”没有身份和动产吗?

c++

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

类型T的多个别名

我想为一个类型创建多个别名,我真正需要的是如下所示,

using MIN = MAX = AVG = nano_t;
Run Code Online (Sandbox Code Playgroud)

(这似乎更优雅,更少打字,而且还有两个以上的情况我必须做这种任务),而不是这样做:

using....
using...
using... every time
Run Code Online (Sandbox Code Playgroud)

但单行分配对我想要的编译器没有意义.有没有其他方法可以做到这一点?

c++ c++11 c++14

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

std :: tuple是否接受auto作为类型

我必须将一个int和一个函数对象返回给调用者,我被认为是返回一个元组,就像make_tuple(int,[](some){})我现在在GCC上那样不支持decltpe(auto)作为返回类型,无论如何我std::tuple<int,auto> myfun()现在可以使我的返回类型我一直在做以下(但不确定)

auto myfun()->decltype(make_tuple(100,p))
{
   auto p=[](some){};
   return make_tuple(100,p);
}
Run Code Online (Sandbox Code Playgroud)

我在做什么好吗?

c++ auto c++11 stdtuple

-3
推荐指数
1
解决办法
732
查看次数