c++ - 您可以将操纵器传递给函数吗?

标签 c++ c++11 iostream iomanip

我想将一个操纵器列表传递给一个函数,如下所示:

void print(const vector<std::smanip>& manips) {
  // ...
  for (auto m : manips)
    cout << m;
  // ...
}

理想情况下,它会被这样的代码调用:

some_object.print({std::fixed, std::setprecision(2)}));

g++ 4.7.0 说:

error: ‘std::smanip’ has not been declared

显然,smanip 并没有在标准中真正定义,C++11 编译器不需要为操纵器类型提供明确的名称。我尝试通过提取已知操纵器来声明类型,如下所示:

typedef decltype(std::fixed) manip;

这打开了一系列新的错误信息,包括这个:

error: ‘const _Tp* __gnu_cxx::new_allocator< <template-parameter-1-1> 
>::address(__gnu_cxx::new_allocator< <template-parameter-1-1> >::const_reference)
const [with _Tp = std::ios_base&(std::ios_base&); __gnu_cxx::new_allocator<
<template-parameter-1-1> >::const_pointer = std::ios_base& (*)(std::ios_base&);
__gnu_cxx::new_allocator< <template-parameter-1-1> >::const_reference =
std::ios_base& (&)(std::ios_base&)]’ cannot be overloaded

我是应该现在就放弃,还是有办法做到这一点?

最佳答案

输出操纵器可以是满足 os << m 的任何类型为某些 basic_ostream 定义实例化。操纵器可以是一个函数(受 operator<<basic_ostream 重载约束)但它也可以是定义自己的 operator<< 的任何类型。 .因此我们需要执行类型删除来捕获 operator<<一个合适的basic_ostream实例化;最简单的方法是使用 std::function和一个 lambda:

#include <iostream>
#include <iomanip>
#include <functional>
#include <vector>

template<typename S>
struct out_manipulator: public std::function<S &(S &)> {
   template<typename T> out_manipulator(T &&t): std::function<S &(S &)>(
      [=](S &i) -> S &{ return i << t; }) {}
   template<typename T> out_manipulator(T *t): std::function<S &(S &)>(
      [=](S &i) -> S &{ return i << t; }) {}    // for g++
   template<typename U> friend U &operator<<(U &u, out_manipulator &a) {
      return static_cast<U &>(a(u));
   }
};

void print(const std::vector<out_manipulator<std::ostream>> &manips) {
   for (auto m: manips)
      std::cout << m;
}

int main() {
   print({std::fixed, std::setprecision(2)});
   std::cout << 3.14159;
}

关于c++ - 您可以将操纵器传递给函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14702629/

相关文章:

c++ - 为什么我们不能从派生类中调用 protected 析构函数?

c++ - 在 C++ std::streams 中,失败后,如何获取失败原因?必需:线程安全且 Windows 和 Linux 通用(或至少 Msvc/Gcc)

c++ - Windows:构建同一 DLL 的 32 位和 64 位版本

c++ - 使固定大小的 block 池分配器适应某些 STL 容器

c++ - 无法在 SDL 2.0 中加载多个纹理

c++11 - C++ 11 中的单例使用成员函数说明符

c++ - C++11 lambda 表达式末尾的括号

c++ - 检测在 C++ 中存储为 double 的负 0

c++ - 如何将类似 iostream 的接口(interface)写入日志库?

c++11 - ios_base::sync_with_stdio(false) 会影响 <fstream> 吗?