在C ++ 11中使用auto和decltype

时间:2009-07-07 18:39:03

标签: c++ templates c++11 decltype auto

我正在尝试学习当前接受的c ++ 11功能,而我遇到auto和decltype问题。作为一个学习练习,我正在使用一些通用函数扩展std类列表。

template<class _Ty, class _Ax = allocator<_Ty>>
class FList : public std::list<_Ty, _Ax>
{
public:
    void iter(const function<void (_Ty)>& f)
    {
        for_each(begin(), end(), f);
    }

    auto map(const function<float (_Ty)>& f) -> FList<float>*
    {
        auto temp = new FList<float>();

        for (auto i = begin(); i != end(); i++)
            temp->push_back(f(*i));

        return temp;
    }
};

auto *ints = new FList<int>();
ints->push_back(2);
ints->iter([](int i) { cout << i; });

auto *floats = ints->map([](int i) { return (float)i; });
floats->iter([](float i) { cout << i; });

对于成员映射,我希望返回类型是通用的,具体取决于传递的函数返回的内容。所以对于返回类型,我可以做这样的事情。

auto map(const function<float (_Ty)>& f) -> FList<decltype(f(_Ty))>*

这还需要删除函数模板中的float类型。

auto map(const function<auto (_Ty)>& f) -> FList<decltype(f(_Ty))>*

我可以使用模板类,但这使得实例的使用更加冗长,因为我必须指定返回类型。

template<class T> FList<T>* map(const function<T (_Ty)>& f)

总结我的问题,我试图弄清楚如何在不使用模板类的情况下定义地图,并且仍然在它返回的类型中使用它。

3 个答案:

答案 0 :(得分:17)

不鼓励从std::list或其他std::容器派生。

将您的操作编写为自由函数,以便它们可以通过迭代器处理任何标准容器。

您的意思是“在不使用模板功能的情况下定义地图”吗?

您应该能够使用result_type std::function成员类型来获取它返回的类型。

此外,您无需指定该函数作为std::function传递。您可以将其保持为任何类型,并让编译器加入所有类型。对于运行时多态性,您只需要std::function

使用new创建原始堆分配对象并通过指针返回它们是soooo 1992! :)

你的iter功能与range-based for loop基本相同。

但除此之外......你的意思是这样吗?

template <class TFunc>
auto map(const TFunc &f) -> FList<decltype(f(_Ty()))>*
{
    auto temp = new FList<decltype(f(_Ty()))>();

    for (auto i = begin(); i != end(); i++)
        temp->push_back(f(*i));

    return temp;
}

这将匹配任何可调用的东西,并使用decltype计算出函数的返回类型。

请注意,它要求_Ty是默认构造的。您可以通过制造实例来解决这个问题:

template <class T>
T make_instance();

不需要实现,因为没有生成调用它的代码,因此链接器没有什么可抱怨的(感谢dribeas指出这个!)

所以代码现在变成了:

FList<decltype(f(make_instance<_Ty>()))>*

或者,从字面上看,通过引用_Ty实例调用函数f可以得到的类型列表。

作为接受的免费奖励,查找右值参考 - 这些意味着你可以写:

std::list<C> make_list_somehow()
{
    std::list<C> result;
    // blah...
    return result;
}

然后像这样称呼它:

std::list<C> l(make_list_somehow());

因为std :: list将有一个“移动构造函数”(就像复制构造函数一样,但是当参数是临时的时候选择,就像这里一样),它可以窃取返回值的内容,即做同样的最优值swap。所以没有复制整个列表。 (这就是为什么C ++ 0x会让天真编写的现有代码运行得更快 - 许多流行但丑陋的性能技巧将会过时)。

你可以免费为自己的任何现有类获得相同类型的东西,而无需使用unique_ptr编写正确的移动构造函数。

std::unique_ptr<MyThing> myThing(make_my_thing_somehow());

答案 1 :(得分:0)

您不能在函数参数中使用auto,因为您希望推导出参数的类型。你使用模板。看一下: http://thenewcpp.wordpress.com/2011/10/18/the-keyword-auto/http://thenewcpp.wordpress.com/2011/10/25/decltype-and-declval/。他们都解释了如何使用auto和decltype。他们应该为您提供有关如何使用它们的足够信息。特别是,关于make_instance的另一个答案可以通过declval更好地完成。

答案 2 :(得分:0)

我认为Jarrah在他的回答中提出的观点是,这些要点确实解释了如何使用这些东西。无论如何,我会指出细节:

你不能这样做,有两件事是错的:

auto map(const function<auto (_Ty)>& f) -> FList<decltype(f(_Ty))>*

auto不能用于函数参数。如果您想要推导出函数的类型,那么您应该使用模板。第二个是decltype表达式,而_Ty是一个类型。以下是解决方法:

template <typename Ret>
auto
map(const function<Ret (_Ty)>& f)
  -> FList<decltype(f(declval<_Ty>()))>
{}

这样就无法创建一个类型的实例。