简体   繁体   English

“忽略候选模板:替换失败:”编译器错误?

[英]"candidate template ignored: substitution failure:" compiler error?

I'm currently trying to write some code with boost::asio, compiling with clang 3.1.我目前正在尝试使用 boost::asio 编写一些代码,并使用 clang 3.1 进行编译。

I have a simple function object:我有一个简单的函数对象:

struct tcp_socket_match_condition
{
    template <typename TIter>
    std::pair<TIter, bool> operator()(TIter begin, TIter end) const
    {
        auto result(std::find(begin, end, '\n'));
        const bool found(result != end);
        return std::make_pair(found ? ++result : end, found);
    }
}; 

I attempt to pass this to the boost::asio::read_until function like so:我尝试将其传递给boost::asio::read_until函数,如下所示:

boost::asio::read_until(socket, stream, match_condition_, error); 

The compiler error generated looks to point out that it can't find the correct function overload.生成的编译器错误似乎指出它找不到正确的函数重载。 Any ideas why this isn't working?任何想法为什么这不起作用?

I've provided the full class and compiler error.我提供了完整的类和编译器错误。

In file included from src/network/admin_socket.cpp:1:
In file included from include/bytes42/arthur/network/admin_socket.hpp:4:
include/bytes42/arthur/network/tcp_socket.hpp:95:21: error: no matching function for call to 'read_until'
                    boost::asio::read_until(socket, stream, match_condition_, error);
                    ^~~~~~~~~~~~~~~~~~~~~~~
src/network/admin_socket.cpp:81:10: note: in instantiation of member function
      'bytes42::arthur::network::tcp_socket<bytes42::arthur::network::detail::tcp_socket_match_condition>::listen' requested here
        socket_.listen();
                ^
/usr/local/include/boost/asio/impl/read_until.hpp:47:13: note: candidate function [with SyncReadStream =
      boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, Allocator =
      std::__1::allocator<char>] not viable: no known conversion from 'bytes42::arthur::network::detail::tcp_socket_match_condition' to 'char' for 3rd
      argument;
std::size_t read_until(SyncReadStream& s,
            ^
/usr/local/include/boost/asio/impl/read_until.hpp:138:13: note: candidate function [with SyncReadStream =
      boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, Allocator =
      std::__1::allocator<char>] not viable: no known conversion from 'bytes42::arthur::network::detail::tcp_socket_match_condition' to
      'const std::string' (aka 'const basic_string<char, char_traits<char>, allocator<char> >') for 3rd argument;
std::size_t read_until(SyncReadStream& s,
            ^
/usr/local/include/boost/asio/impl/read_until.hpp:203:13: note: candidate function [with SyncReadStream =
      boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, Allocator =
      std::__1::allocator<char>] not viable: no known conversion from 'bytes42::arthur::network::detail::tcp_socket_match_condition' to
      'const boost::regex' (aka 'const basic_regex<char, regex_traits<char> >') for 3rd argument;
std::size_t read_until(SyncReadStream& s,
            ^
/usr/local/include/boost/asio/impl/read_until.hpp:260:13: note: candidate template ignored: substitution failure [with SyncReadStream =
      boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, Allocator =
      std::__1::allocator<char>, MatchCondition = bytes42::arthur::network::detail::tcp_socket_match_condition]
std::size_t read_until(SyncReadStream& s,
            ^
/usr/local/include/boost/asio/impl/read_until.hpp:312:20: note: candidate template ignored: substitution failure [with SyncReadStream =
      boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >, Allocator =
      std::__1::allocator<char>, MatchCondition = bytes42::arthur::network::detail::tcp_socket_match_condition]
inline std::size_t read_until(SyncReadStream& s,
                   ^
/usr/local/include/boost/asio/impl/read_until.hpp:37:20: note: candidate function template not viable: requires 3 arguments, but 4 were provided
inline std::size_t read_until(SyncReadStream& s,
                   ^
/usr/local/include/boost/asio/impl/read_until.hpp:93:20: note: candidate function template not viable: requires 3 arguments, but 4 were provided
inline std::size_t read_until(SyncReadStream& s,
                   ^
/usr/local/include/boost/asio/impl/read_until.hpp:193:20: note: candidate function template not viable: requires 3 arguments, but 4 were provided
inline std::size_t read_until(SyncReadStream& s,
                   ^
1 error generated.
make: *** [build/src/network/admin_socket.o] Error 1

Class:班级:

    template <typename TMatchCondition>
    class tcp_socket
    {
        public:
            typedef std::function<std::string(const std::string&)> data_callback;

        public:
            tcp_socket(
                const unsigned short port,
                TMatchCondition match_condition,
                data_callback callback);

            void listen();
            void stop();

        private:
            tcp_socket(const tcp_socket&)   = delete;
            tcp_socket(tcp_socket&&)        = delete;

            tcp_socket& operator=(const tcp_socket&) = delete;
            tcp_socket& operator=(tcp_socket&&)      = delete;

        private:
            const utils::entry_exit         entry_exit_;
            boost::asio::io_service         service_;
            boost::asio::ip::tcp::acceptor  acceptor_;
            TMatchCondition                 match_condition_;
            data_callback                   callback_;
    };


    template <typename TMatchCondition>
    tcp_socket<TMatchCondition>::tcp_socket(
        const unsigned short port,
        TMatchCondition match_condition,
        data_callback callback)
        : entry_exit_("tcp_socket:" + std::to_string(port))
        , acceptor_(service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
        , match_condition_(match_condition)
        , callback_(callback) {}

    template <typename TMatchCondition>
    void tcp_socket<TMatchCondition>::listen()
    {
        const auto port(acceptor_.local_endpoint().port());
        const std::string port_str(std::to_string(port));

        while(acceptor_.is_open())
        {
            boost::system::error_code error;

            utils::entry_exit ee("Listening on port " + port_str);

            boost::asio::ip::tcp::socket socket(service_);
            acceptor_.accept(socket, error);

            if(error)
            {
                if(error != boost::asio::error::bad_descriptor)
                {
                    LOG(ERROR)
                        << "An error occured while trying to accept a client connection; error="
                        << error.message();

                    sleep(1);  // don't want to flood logs
                }
            }
            else
            {
                while(socket.is_open())
                {
                    boost::asio::streambuf stream;
                    boost::asio::read_until(socket, stream, match_condition_, error);

                    const std::string msg(
                        (std::istreambuf_iterator<char>(&stream)),
                        std::istreambuf_iterator<char>());

                    LOG(INFO) << "Received message: " << msg;

                    boost::asio::write(
                        socket,
                        boost::asio::buffer(callback_(msg)),
                        error);

                    if(error)
                    {
                        if(error != boost::asio::error::broken_pipe)
                        {
                            LOG(ERROR)
                                << "Error whilst writing response, closing client connection: "
                                << error.message();
                        }

                        socket.close();

                        sleep(1);  // don't want to flood logs
                    }
                }
            }
        }
    }


    template <typename TMatchCondition>
    void tcp_socket<TMatchCondition>::stop()
    {
        boost::system::error_code error;
        acceptor_.close(error);

        if(error)
        {
            LOG(ERROR) << "Error whilst stopping TCP socket; error=" << error.message();
        }
    }                                

我认为您没有发布完整代码,但问题似乎是匹配条件:您是否使用boost::is_match_condition指定您的tcp_socket_match_condition是匹配条件?

Ok;好的; so clang is being (kinda) helpful here.所以clang在这里(有点)有帮助。

it's telling you that there are eight specializations of read_until , and that it couldn't make any of them work.它告诉您read_until八个专业化,并且它无法使它们中的任何一个工作。

The first three take a char, char, and std::string for the third parameter.前三个采用 char、char 和 std::string 作为第三个参数。 That won't work.那行不通。

The last three have three parameters - you're passing four.最后三个有三个参数 - 你传递了四个。 Those worn't work either.那些穿的也不行。

That leaves the two in the middle.这让两个人处于中间。 For some reason, your TMatchCondition is generating a substitution failure.出于某种原因,您的TMatchCondition正在生成替换失败。

As Dietmar just pointed out, you should definitely check to see if boost::is_match_condition<TMatchCondition>::value is true - because it won't work if it is not.正如 Dietmar 刚刚指出的那样,您绝对应该检查boost::is_match_condition<TMatchCondition>::value是否为 true - 因为如果不是,它将无法工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 候选模板被忽略:替换失败(clang但不是g ++错误) - candidate template ignored: substitution failure(error with clang but not g++) “候选模板被忽略:替换失败”错误帮助,C ++,犰狳,rank(), - 'candidate template ignored: substitution failure' error help, C++, Armadillo, rank(), clang: 候选模板被忽略: 替换失败: typedef &#39;type&#39; 不能用类说明符引用 - clang: candidate template ignored: substitution failure: typedef 'type' cannot be referenced with a class specifier 忽略Clang候选模板:替换失败(也无法使用gcc编译,VC工作正常) - Clang candidate template ignored: substitution failure (also fails to compile with gcc, VC works fine) 为什么编译器会说“候选模板被忽略:无法推断模板参数&#39;InputIterator&#39;”? - Why compiler said “candidate template ignored: couldn't infer template argument 'InputIterator'”? 替换失败的模板特化 - Template specialisation on substitution failure 模板模板参数的替换失败 - Substitution failure for template template argument 如何解决错误“候选模板被忽略:无法推断模板参数&#39;T&#39;”? - How to solve error “candidate template ignored: couldn't infer template argument 'T' ”? 运算符重载代码编译错误,模板参数推导/替换失败 - Operator overloading code compilation error, template argument deduction/substitution failure 替换失败是否是具有相关非类型模板参数的错误? - Is substitution failure an error with dependent non-type template parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM