Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1054049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:20:42+00:00 2026-05-16T17:20:42+00:00

Using visual studio 2008 with the tr1 service pack and Intel C++ Compiler 11.1.071

  • 0

Using visual studio 2008 with the tr1 service pack and Intel C++ Compiler 11.1.071 [IA-32], this is related to my other question

I’m attempting to write a functional map for c++ which would work somewhat like the ruby version

strings = [2,4].map { |e| e.to_s }

So i’ve defined the following function in the VlcFunctional namespace

template<typename Container, typename U>
vector<U> map(const Container& container, std::tr1::function<U(Container::value_type)> f)
{
    vector<U> transformedValues(container.size());
    int index = -1; 
    BOOST_FOREACH(const auto& element, container)
    {
        transformedValues.at(++index) = f(element);
    }
    return transformedValues; 
}

and you can call this like so (Note that the function template arguments are defined explicitly):

vector<int> test;
test.push_back(2); test.push_back(4); 
vector<string> mappedData2 = VlcFunctional::map<vector<int>,string>(test, [](int i) -> string
{
    return ToString(i);
});

Or like so (Note that the function template arguments aren’t defined explicitly)

std::tr1::function f = [](int i) -> string { return ToString(i); };
vector<string> mappedData2 = VlcFunctional::map<vector<int>,string>(test, f);

But crucially, NOT LIKE THIS

vector<string> mappedData2 = VlcFunctional::map(test, [](int i) -> string { return ToString(i); });

Without the explicit definition of hte template arguments, it doesn’t know which template to use and falls over with a compile error

 ..\tests\VlcFunctional_test.cpp(106): error: no instance of function template "VlcFunctional::map" matches the argument list, argument types are: (std::vector<int, std::allocator<int>>, __lambda3)

Having to define the template arguments makes it a much more bulky syntax and I’m aiming for minimal cruft at the call site – any ideas on why it doesn’t know how do the conversion? Is this a compiler issue or does the language not allow for this type of template argument inference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:20:43+00:00Added an answer on May 16, 2026 at 5:20 pm

    The problem is that a lambda is not a std::function even if it can be converted. When deducing type arguments, the compiler is not allowed to perform conversions on the actual provided arguments. I would look for a way to have the compiler detect the type U and let the second argument free for the compiler to deduce:

    template <typename Container, typename Functor>
    std::vector< XXX > VlcFunctional::map( Container &, Functor )...
    

    Now the issue is what to write in XXX. I don’t have the same compiler that you do, and all C++0x features are still a little tricky. I would first try to use decltype:

    template <typename Container, typename Functor>
    auto VlcFunctional::map( Container & c, Functor f ) -> std::vector< decltype(f(*c.begin())) > ...
    

    Or maybe type traits if the compiler does not support decltype yet.

    Also note that the code you are writting is quite unidiomatic in C++. Usually when manipulating containers the functions are implemented in terms of iterators, and your whole map is basically the old std::transform:

    std::vector<int> v = { 1, 2, 3, 4, 5 };
    std::vector<std::string> s;
    std::transform( v.begin(), v.end(), std::back_inserter(s), [](int x) { return ToString(x); } );
    

    Where std::transform is the C++ version of your map function. While the syntax is more cumbersome, the advantage is that you can apply it to any container, and produce the output to any other container, so the transformed container is not fixed to std::vector.

    EDIT:
    A third approach, probably easier to implement with your current compiler support is manually providing just the return type of the lambda as template argument, and letting the compiler deduce the rest:

    template <typename LambdaReturn, typename Container, typename Functor>
    std::vector<LambdaReturn> map( Container const & c, Functor f )
    {
       std::vector<LambdaReturn> ret;
       std::transform( c.begin(), c.end(), std::back_inserter(ret), f );
       return ret;
    }
    int main() {
       std::vector<int> v{ 1, 2, 3, 4, 5 };
       auto strs = map<std::string>( v, [](int x) {return ToString(x); });
    }
    

    Even if you want to add syntactic sugar to your map function, there is no need to manually implement it when you can use existing functionality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Visual Studio 2008 with Feature Pack 1. I have a typedef like
Using Visual Studio 2008 Team Edition, is it possible to assign a shortcut key
Using Visual Studio 2008 / C# / VS Unit Testing. I have a very
I'm looking into using Visual Studio 2008's built in unit test projects instead of
I'm using visual studio 2008 and ReSharper 4 and it's kind of slow. My
I've started using Visual Studio 2008 and it keeps asking me to upgrade my
I'm using Visual Studio 2008 and the built-in installation tools for a C# client
I'm using Visual Studio 2008 Team System with SP1, and I've noticed an annoying
If I'm using Visual Studio 2008 targeted to ASP.NET 2.0, then which version of
Applications I am using: Visual Studio 2008 (C#/ASP.NET) Visual Source Safe 8.0 IIS 5.1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.