Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

c++ - How to use template within template in C++11?

I use boost to create a websocket server. Because we need ws and wss, two versions, so I write code like below. But when I build it, said "error C2338: Unknown Socket type in async_teardown."

I guess it's becuuse T1 .

file 1 iSession.h '''

//
// Created by p00451414 on 2019/5/14.
//

#ifndef ESDK_MSP_ISESSION_H
#define ESDK_MSP_ISESSION_H
#pragma once
#include "stdfax.h"


template<typename T>
class iSession :public std::enable_shared_from_this <iSession<T>> {
public:
    /*explicit
        iSession(boost::asio::ip::tcp::socket);
    explicit
        iSession(boost::asio::ip::tcp::socket, boost::asio::ssl::context&);*/

    iSession() {
        ws_ = NULL;
        strand_ = NULL;
        buffer_ = new boost::beast::multi_buffer();
    }
    ~iSession() {
        delete buffer_;
        buffer_ = NULL;
        delete strand_;
        strand_ = NULL;
        delete ws_;
        ws_ = NULL;

    }

public:
    boost::beast::websocket::stream<T> *ws_=NULL;
    boost::asio::strand<boost::asio::io_context::executor_type> *strand_=NULL;
    boost::beast::multi_buffer *buffer_=NULL;
public:
    void virtual run() = 0;

    void process() {
        auto sp = shared_from_this();

        ws_->async_read(*buffer_, boost::asio::bind_executor(*strand_, [&, sp](boost::system::error_code ec2,
            std::size_t bytes_transferred2) {
            boost::ignore_unused(bytes_transferred2);
            if (ec2) {
                //   fail(ec, "read");
            }


            if (ec2 == boost::beast::websocket::error::closed) {
                return;
            }
            ws_->text(ws_->got_text());
            ws_->async_write(buffer_->data(), boost::asio::bind_executor(*strand_, [&, sp](boost::system::error_code ec3,
                std::size_t bytes_transferred3) {
                boost::ignore_unused(bytes_transferred3);
                if (ec3) {
                    //   return fail(ec, "write");
                }


                // Clear the buffer
                buffer_->consume(buffer_->size());
                process();
            }));

        }));
    }

};


#endif //ESDK_MSP_ISESSION_H

'''

file2 NormalSession.h '''

//
// Created by p00451414 on 2019/5/14.
//

#ifndef ESDK_MSP_NORMALSESSION_H
#define ESDK_MSP_NORMALSESSION_H



#include "iSession.h"

class NormalSession : public iSession<boost::asio::ip::tcp::socket> {
public:
    NormalSession(boost::asio::ip::tcp::socket);

    ~NormalSession();

public:


    void run();
};


#endif //ESDK_MSP_NORMALSESSION_H

'''

file 3 SslSession.h '''

//
// Created by p00451414 on 2019/5/14.
//

#ifndef ESDK_MSP_SSLSESSION_H
#define ESDK_MSP_SSLSESSION_H


#include <memory>
#include <boost/asio/ip/tcp.hpp>


#include "iSession.h"


class SslSession : public iSession<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>> {
public:
    SslSession(boost::asio::ip::tcp::socket, boost::asio::ssl::context &);

    ~SslSession(void);

public:
    void on_handshake(boost::system::error_code);

    //void on_accept(boost::system::error_code);

    //void do_read();

    //void on_read(boost::system::error_code, std::size_t);

    //void on_write(boost::system::error_code, std::size_t);

    void run();

private:
    boost::asio::ip::tcp::socket* socket_;
};


#endif //ESDK_MSP_SSLSESSION_H

'''

I guess it's becuuse my SslSession with T1 template. Because my class NormalSession could run when SslSession commented.

So how to fix this error and how to use template within template?

more Msg

more msg

'''

F:oost_1_69_0oost_1_69_0oost/beast/websocket/teardown.hpp(103): error C2338: Unknown Socket type in async_teardown. F:oost_1_69_0oost_1_69_0oost/beast/websocket/impl/read.ipp(680): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::async_teardown::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream::read_op>::process::,boost::asio::strand>>>>(boost::beast::websocket::role_type,Socket &,TeardownHandler &&)”的引用 with [ NextLayer=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer, Socket=boost::asio::ssl::stream, TeardownHandler=boost::beast::websocket::stream,true>::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream,true>::read_op>::process::,boost::asio::strand>>> ] F:oost_1_69_0oost_1_69_0oost/beast/websocket/impl/read.ipp(173): note: 编译 类 模板 成员函数 "void boost::beast::websocket::stream::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>>::operator ()(boost::beast::error_code,size_t,bool)" 时 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:oost_1_69_0oost_1_69_0oost/beast/websocket/impl/read.ipp(821): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::stream::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>>::operator ()(boost::beast::error_code,size_t,bool)”的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:oost_1_69_0oost_1_69_0oost/beast/websocket/impl/read.ipp(821): note: 参见对正在编译的 类 模板 实例化 "boost::beast::websocket::stream::read_some_op>::mutable_buffers_type,boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>>" 的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:oost_1_69_0oost_1_69_0oost/beast/websocket/impl/read.ipp(802): note: 编译 类 模板 成员函数 "void boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>::operator ()(boost::beast::error_code,size_t)" 时 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:oost_1_69_0oost_1_69_0oost/beast/websocket/impl/read.ipp(899): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>::operator ()(boost::beast::error_code,size_t)”的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] F:oost_1_69_0oost_1_69_0oost/beast/websocket/impl/read.ipp(899): note: 参见对正在编译的 类 模板 实例化 "boost::beast::websocket::stream::read_op::process::,boost::asio::strand>>" 的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer ] f:mspesdk_icpsourceesdk_mspmsp_ssrcwebsocketiSession.h(69): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::stream::async_read::process::,boost::asio::strand>>(DynamicBuffer &,ReadHandler &&)”的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer, ReadHandler=boost::asio::executor_binder>::process::,boost::asio::strand> ] f:mspesdk_icpsourceesdk_mspmsp_ssrcwebsocketiSession.h(44): note: 参见对正在编译的函数 模板 实例化“void boost::beast::websocket::stream::async_read::process::,boost::asio::strand>>(DynamicBuffer &,ReadHandler &&)”的引用 with [ T=boost::asio::ssl::stream, DynamicBuffer=boost::beast::multi_buffer, ReadHandler=boost::asio::executor_binder>::process::,boost::asio::strand> ] f:mspesdk_icpsourceesdk_mspmsp_ssrcwebsocketiSession.h(41): note: 编译 类 模板 成员函数 "void iSession>::process(void)" 时 F:MSPeSDK_ICPSourceeSDK_MSPMSP_SsrcwebsocketSslSession.cpp(44): note: 参见对正在编译的函数 模板 实例化“void iSession>::process(void)”的引用 f:mspesdk_icpsourceesdk_mspmsp_ssrcwebsocketSslSession.h(16): note: 参见对正在编译的 类 模板 实例化 "iSession>" 的引用 F:oost_1_69_0oost_1_69_0oost/beast/core/string.hpp(103): note: 参见对正在编译的 类 模板 实例化 "boost::basic_string_view>" 的引用 NMAKE : fatal error U1077: “D:VS2017VCToolsMSVC14.16.27023inHostx64x64cl.exe”: 返回代码“0x2” Stop. NMAKE : fatal error U1077: “D:VS2017VCToolsMSVC14.16.27023inHostx64x64 make.exe”: 返回代码“0x2” Stop. NMAKE : fatal error U1077: “D:VS2017VCToolsMSVC14.16.27023inHostx64x64 make.exe”: 返回代码“0x2” Stop. NMAKE : fatal error U1077: “D:VS2017VCToolsMSVC14.16.27023inHostx64x64 make.exe”: 返回代码“0x2” Stop.

'''

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Got it ,after read the source code, I read the comments in teardown.hpp, I forget include #include <boost/beast/websocket/ssl.hpp>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...