不能将typeof(std :: endl)作为模板参数? [英] Cannot have typeof(std::endl) as template parameter?

查看:154
本文介绍了不能将typeof(std :: endl)作为模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图写一个这样的函数:

So, I was trying to write a function like this:

void append_to_stream(std::ostream &stream)
{ }

template <typename T, typename... Args>
void append_to_stream(std::ostream &stream, T first, Args&&... rest)
{
  stream << first;
  append_to_stream(stream, rest...);
}

并调用它:

append_to_stream(stream, 
                 std::endl,
                 std::endl);

但这不起作用。我得到一个错误,说'太多的参数'的函数。我把它缩小到我知道 std :: endl 是有罪的 - 可能是因为它是一个函数。我设法通过声明一个 endl 的结构并定义< 它只是调用 std :: endl 。这工作,但不觉得特别好。是不是可以接受std :: endl作为模板参数?该功能适用​​于其他类型。

But this doesn't work. I get an error that says 'too many arguments' to the function. I've narrowed it down to the point I know that the std::endl is guilty - probably because it's a function. I managed to 'solve' this by declaring a struct called endl and define the <<operator for it so that it simply calls std::endl. This works but doesn't feel particularly good. Is it not possible to accept std::endl as a template argument? The function works for other types.

编辑:这是错误:

src/log/sinks/file_sink.cpp:62:21: error: too many arguments to function ‘void log::sinks::append_to_stream(std::string&, Args&& ...) [with Args = {}, std::string = std::basic_string<char>]’



<更新

尝试让编译器推导出正确的模板参数@MooingDuck建议使用以下形式的函数:

Trying to get the compiler to deduce the correct template arguments @MooingDuck suggested that a function of the following form could be used:

  template<class e, class t, class a> 
  basic_ostream<e,t>&(*)(basic_ostream<e,t>&os) get_endl(basic_string<e,t,a>& s) 
  {
return std::endl<e,t>;
  }

但这不会编译。

错误:

src/log/sinks/file_sink.cpp:42:28: error: expected unqualified-id before ‘)’ token
src/log/sinks/file_sink.cpp:42:53: error: expected initializer before ‘get_endl’

任何想法为什么?为了编译这个,我添加了 using namespace std;

Any ideas why? For the sake of compiling this, I've added using namespace std;

推荐答案

std :: endl 是一个模板,不是函数,编译器无法解析 endl

std::endl is a template, not a function, and the compiler cannot resolve which endl to use.

尝试:

append_to_stream(std::cout,
             std::endl<char, std::char_traits<char>>,
             std::endl<char, std::char_traits<char>>);

或者,MooingDuck的解决方案(更正):

Or, MooingDuck's solution (corrected):

template<class e, class t, class a> //string version
std::basic_ostream<e, t>& (*get_endl(const std::basic_string<e, t, a>&))
    (std::basic_ostream<e, t>& )
{ return std::endl<e,t>; } 

template<class e, class t> //stream version
std::basic_ostream<e, t>& (*get_endl(const std::basic_ostream<e, t>&))
    (std::basic_ostream<e, t>& )
{ return std::endl<e,t>; }

int main () {
  std::ostream& stream = std::cout;
  append_to_stream(stream,
                 get_endl(stream),
                 get_endl(stream));
}

这里是get_endl解决方案,由C ++ 11简化 decltype feature:

Here is get_endl solution, simplified by C++11 decltype feature:

template<class e, class t, class a> //string version
auto get_endl(const std::basic_string<e, t, a>&)
  -> decltype(&std::endl<e,t>)
{ return std::endl<e,t>; }

template<class e, class t> //stream version
auto get_endl(const std::basic_ostream<e,t>&)
  -> decltype(&std::endl<e,t>)
{ return std::endl<e,t>; }

int main () {
  std::ostream& stream = std::cout;
  append_to_stream(stream,
                 get_endl(stream),
                 get_endl(stream));
}

这篇关于不能将typeof(std :: endl)作为模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆