是否有人编写了脚本、插件或可执行文件,将“auto”的每个实例替换为编译器推断的类型?我需要移植一些到处使用 auto 的 C++11 代码。
Clang 是我的第一个候选人。有没有人修改它来做这样的事情?
另一种方法是从编译器解析错误,因为预期的类型可能在错误输出中。我可以 -Dauto=int
并可能返回 "could not convert std::vector<int>::iterator to 'int'"
最佳答案
不幸的是,这在一般情况下是不可能的。考虑:
template <typename T> void foo(T & t)
{
auto it = t.find(42);
...
}
...
std::map<int, int> m;
std::set<int> s;
...
foo(m);
foo(s);
诚然,这是一个毫无意义的例子,但它表明当依赖于模板参数时,无法知道用什么替换 auto。 std::map
和 std::set
, 顺便说一下,包含相同名称的 typedef ( iterator
) 代表各自迭代器的类型,所以 typename T::iterator it
可以在这里工作,但你可以实例化 foo
对于 T
没有这样的 typedef。
标准库类中的大量 typedef 完全是为了允许在 auto
之前编写此类模板。被发明/重新使用,您可以做同样的事情来处理没有 auto
的编译器.但这不是您可以自动化的事情,至少不能不付出与添加对 auto
的支持相当的努力。到编译器...
即使 auto
不依赖于模板类型,用对用户有意义且可移植的东西替换它是一个难题。采取:
std::map<int, int> m;
auto it = m.find(42);
auto
的合理替换是 std::map<int, int>::iterator
, 但是如果你使用 -Dauto=int
并查看编译器错误消息,您可以将其替换为 std::_Rb_tree_iterator<std::pair<const int, int> >
.那是标准库的实现细节,难以阅读,而且显然不可移植——你不想在你的代码中那个。
在你的例子中,我的编译器 (GCC 4.4.6) 说:
error: cannot convert
__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >
toint
in initialization
关于c++ - 将 auto 关键字替换为推导类型(clang 或 VS2010),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8935590/