template argument deduction of return type in function template

Tobias Hermann

Given the following minimal example:

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <set>
#include <vector>

template<typename ContainerOut, typename X, typename Y, typename ContainerIn>
ContainerOut Map( const ContainerIn& xs, const std::function<Y( const X& )>& f )
{
    ContainerOut ys;
    std::transform( begin( xs ), end( xs ), std::inserter( ys, end( ys ) ), f );
    return ys;
}

struct Foo {
    Foo( int val ) : val_( val ) {}
    int val_;
};

std::set<int> FooValsToIntSet( const std::list<Foo>& foos )
{
    //Map<std::set<int>, Foo, int>
    return Map( foos, []( const Foo& foo )
    {
        return foo.val_;
    } );
}

int main()
{
    std::list<Foo> foos = { 1, 2, 2, 3 };
    std::set<int> vals = FooValsToIntSet( foos );
    for ( auto& v : vals )
        std::cout << v << std::endl;
}

The line

return Map( foos, []( const Foo& foo )

is not accepted by the compilers I tested.

It works if I write out the template arguments explicitly instead:

return Map<std::set<int>, Foo, int>( foos, []( const Foo& foo )

But I do not understand why this is necessary. Is there a way to avoid this verbosity?

Rostislav

Well, the compiler has no way of deducing the ContainerOut type - the return type doesn't participate in type deduction. However, you could let the compiler deduce everything except the return type. Just as a side note, at least in this case, there is no reason to use std::function - it's just adding unnecessary overhead.

This would be better:

template<typename ContainerOut, typename ContainerIn, typename F>
ContainerOut Map( const ContainerIn& xs, F&& f )
{
    ContainerOut ys;
    std::transform( begin( xs ), end( xs ), std::inserter( ys, end( ys ) ), f );
    return ys;
}

and then you can call it as

return Map<std::set<int>>( foos, []( const Foo& foo )
{
    return foo.val_;
} );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

template argument deduction of return type in function template

From Dev

C++ use function argument type for template argument deduction

From Dev

Template argument type deduction won't work within a function object

From Dev

Template type deduction with std::function

From Dev

function template parameter deduction of template parameter vs of default template parameter vs of return type

From Dev

Return type of function call on template argument class

From Dev

Template argument type deduction by conversion operator

From Dev

Template argument deduction from inherited type

From Dev

Template argument deduction from inherited type

From Dev

Template type deduction in parameter and return type

From Dev

function templates, partial application and template argument deduction

From Dev

`std::function` template argument deduction/substitution failed

From Dev

function templates, partial application and template argument deduction

From Dev

function template deduction using raw pointer as argument

From Dev

Variadic template function where return type depends on template argument list

From Dev

Function template argument deduction with variadic class template as function call parameter

From Dev

Template return type deduction from lvalue?

From Dev

Understanding template argument deduction

From Dev

Template argument deduction fails

From Dev

Template argument deduction for 'char*'

From Dev

Template argument deduction order

From Dev

Template argument deduction for 'char*'

From Dev

Choose between template function and auto type deduction

From Dev

C++ template function type deduction

From Dev

C++ template function type deduction

From Dev

Choose between template function and auto type deduction

From Dev

template argument deduction for const argument

From Dev

infer template argument type from function return type

From Dev

infer template argument type from function return type

Related Related

  1. 1

    template argument deduction of return type in function template

  2. 2

    C++ use function argument type for template argument deduction

  3. 3

    Template argument type deduction won't work within a function object

  4. 4

    Template type deduction with std::function

  5. 5

    function template parameter deduction of template parameter vs of default template parameter vs of return type

  6. 6

    Return type of function call on template argument class

  7. 7

    Template argument type deduction by conversion operator

  8. 8

    Template argument deduction from inherited type

  9. 9

    Template argument deduction from inherited type

  10. 10

    Template type deduction in parameter and return type

  11. 11

    function templates, partial application and template argument deduction

  12. 12

    `std::function` template argument deduction/substitution failed

  13. 13

    function templates, partial application and template argument deduction

  14. 14

    function template deduction using raw pointer as argument

  15. 15

    Variadic template function where return type depends on template argument list

  16. 16

    Function template argument deduction with variadic class template as function call parameter

  17. 17

    Template return type deduction from lvalue?

  18. 18

    Understanding template argument deduction

  19. 19

    Template argument deduction fails

  20. 20

    Template argument deduction for 'char*'

  21. 21

    Template argument deduction order

  22. 22

    Template argument deduction for 'char*'

  23. 23

    Choose between template function and auto type deduction

  24. 24

    C++ template function type deduction

  25. 25

    C++ template function type deduction

  26. 26

    Choose between template function and auto type deduction

  27. 27

    template argument deduction for const argument

  28. 28

    infer template argument type from function return type

  29. 29

    infer template argument type from function return type

HotTag

Archive