我想将一个操纵器列表传递给一个函数,如下所示:
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/