包扩展期间无法解决过载问题

时间:2019-06-18 20:49:34

标签: c++11 visual-c++ variadic-templates

当我尝试对代码进行一些修改时,我遇到了包扩展的问题,理论上应该在包扩展中进行编译。

1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(20,2): error C2672:  'ExpandArgs': no matching overloaded function found
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(17): message :  see reference to function template instantiation 'void `anonymous-namespace'::ExpandArgs<const char(&)[9],>(boost::log::v2s_mt_nt6::record_ostream &,Arg)' being compiled
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(17): message :         with
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(17): message :         [
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(17): message :             Arg=const char (&)[9]
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(17): message :         ]
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(54): message :  see reference to function template instantiation 'void `anonymous-namespace'::ExpandArgs<const char(&)[9],std::string&>(boost::log::v2s_mt_nt6::record_ostream &,Arg,std::string &)' being compiled
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(54): message :         with
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(54): message :         [
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(54): message :             Arg=const char (&)[9]
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(54): message :         ]
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(77): message :  see reference to function template instantiation 'void CustomLogger::Log<boost::log::v2s_mt_nt6::sources::logger_mt,const char(&)[9],std::string&>(Logger &,const char (&)[9],std::string &)' being compiled
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(77): message :         with
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(77): message :         [
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(77): message :             Logger=boost::log::v2s_mt_nt6::sources::logger_mt
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(77): message :         ]
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(88): message :  see reference to function template instantiation 'void sA::Log<const char(&)[9],std::string&>(const char (&)[9],std::string &)' being compiled

1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(17,1): error C2780:  'void `anonymous-namespace'::ExpandArgs(boost::log::v2s_mt_nt6::record_ostream &,Arg &&,Args &&...)': expects 3 arguments - 1 provided
1>C:\Projects\PocTemplateOverloadingResolution\Source.cpp(16): message :  see declaration of '`anonymous-namespace'::ExpandArgs'

代码:Link to code

我正在使用Visual Studio 2019(v142) SDK版本:10.0.18362.0

有人可以看到问题吗?

1 个答案:

答案 0 :(得分:0)

  

有人可以看到问题吗?

不确定(对不起:没有提升的经验),但是...

template< typename Arg, typename ...Args >
void ExpandArgs(boost::log::record_ostream& stream, Arg&& arg, Args&&... args)
{
    ExpandArgs(stream, std::forward< Arg >(arg));

    ExpandArgs(stream, std::forward<Args>(args)...); // SHOULD COMPILE, right?
    //ExpandArgs(stream, "xpto", std::string("xpto"));
}

template< typename Arg >
void ExpandArgs(boost::log::record_ostream& stream, Arg&& arg)
{
    stream << std::forward< Arg >(arg);
}

Args...为空时,在“ SHOULD COMPILE,对吗?”中,该怎么办?打电话?

ExpandArgs(stream, std::forward<Args>(args)...); // ???

通话变为

ExpandArgs(stream);

但是我看不到ExpandArgs()函数仅接收流参数。

我想您可以按如下所示添加接线盒

void ExpandArgs (boost::log::record_ostream &)
 { }

解决这个问题。