我想把一系列的操作器传递给一个函数,就像这样:
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
我现在就应该放弃吗,还是有办法可以做到?
std::bind
来处理需要参数的操作器)。 - Jerry Coffin