C++ cout输入模板

C++ cout输入模板,c++,templates,stream,cout,C++,Templates,Stream,Cout,我想创建一个函数,该函数接受任何std::endl实际上是函数模板的内容。您可以阅读完整的文档或。其定义如下: template< class CharT, class Traits > std::basic_ostream<charT,traits>& endl( std::basic_ostream<CharT, Traits>& os ); 模板 std::basic_ostream&endl(std::basic_ostream&os

我想创建一个函数,该函数接受任何
std::endl
实际上是函数模板的内容。您可以阅读完整的文档或。其定义如下:

template< class CharT, class Traits >
std::basic_ostream<charT,traits>& endl( std::basic_ostream<CharT, Traits>& os );
模板
std::basic_ostream&endl(std::basic_ostream&os);
编辑:你可以使用这个解决方案实现你想要的(我模糊地改编自)

#包括
//处理其他类型
模板
作废我的打印(常量T&T){

重载运算符时,std::endl的不可能复制类型未知,因为endl不是函数…@KerrekSB-Huh?
ostream&operator@Nickstd::endl确实是一个函数。@尼克:它是一个函数模板,可以实例化以匹配
ostream
重载。但这与函数不同,因为你刚刚体验过自己。那不是一个函数,那是一个模板。从技术上讲,它被称为IO操纵器。但它是“接受参数并返回值的东西…”,将其称为函数并不遥远…我的帖子中的文档链接也将其列为函数。 error: no matching function for call to 'my_print(<unresolved overloaded function type>)' note: candidate is note: template<class T> void my_print(const T&)
template< class CharT, class Traits >
std::basic_ostream<charT,traits>& endl( std::basic_ostream<CharT, Traits>& os );
#include <iostream>

// handles the other types
template <typename T>
void my_print(const T &t) {
  std::cout << t;
}

// alias a few things to make the prototypes readable
typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
typedef CoutType& (*StandardEndLine)(CoutType&);

int main() {
  my_print("hello\n"); // works
  my_print(4); // works
  my_print((StandardEndLine)std::endl); // <- NOTE: there is an explicit cast
  return 0;
}