0

I would like to create a generic function that accept as parameters a generic List and a Function(T element), giving out the selected element of the list.

I know how to do this in C# but I'm wondering if there is the same functionality in C++ 10 (because I have to write it in an old program), to avoid to replicate code an infinite amount of times (as already is in the program).

// C# like code
T DisplayList<T>(List<T>& myList, funct<T>(T activeElement))
{
    foreach(T element in myList)
    {
        println(funct(element));
    }
    T selectedElement = readln();
    return selectedElement;
}

// the delegate should be specific "format" function for the T element
delegate string funct<T>(T element);
Shyguy
  • 77
  • 8
  • 1
    What language standard is used in the project? C++98 or C++11 (if that is supported by VS2010)? C++ 10 doesn't exist. – Ted Lyngmo Jul 26 '19 at 10:41
  • 1
    You will need to pick up a good C++ book and learn how to use templates. Templates are one of the most advanced C++ topics and cannot be fully explained in one or two paragraphs on stackoverflow.com. The only way to thoroughly learn C++ is with a good book. C++ is not C#. C++'s templates may look like C#'s "generics", but they are completely different fundamental concepts, and they work in fundamentally different ways. [Find yourself a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Jul 26 '19 at 10:42
  • Thank you @SamVarshavchik for your totally nonconstructive answer. As anybody could immagine I am quite new to C++ powerful structures, that's why I was asking. I didn't even knew about templates. Now that someone else has given me a constructive hint I will take a good book and learn what I need to grow up from a good starting point. – Shyguy Jul 29 '19 at 07:40
  • I trust that you were not planning to take the same approach to learn all of C++. It takes 3-5 years of intense study to acquire proficient C++ programming knowledge and skills. Doing it piecemeal, by asking "How do you do X in C++ which is like Y in C#" on stackoverflow.com and waiting for an answer will take ...a little bit longer. The answer here barely scratches the surface, and doesn't even cover such C++ wonders as template specialization, variadic templates, etc... These kinds of one-shot answers are woefully insufficient. You are free not to believe me, of course, it's your choice. – Sam Varshavchik Jul 29 '19 at 12:37

1 Answers1

0

What you need is a template function with an std::function

#include<iostream>
template<type T>
T DisplayList(const std::vector<T>& myList, std::function<std::string(const T&)>)
{
     const auto s = mylist.size();
     for(size_t i = 0; i < s; i++)
     { 
           const auto element = myList[i];
           std::cout << funct(element) << '\n'; 
      }
     T selectedElement = readln(); // I think you need std::in for that
     return selectedElement;
}

You can call the function with a function or an lambda function.

I think you don't need the return statement.

Range based for loops are available in vs 2012 and above.

Michael Aigner
  • 4,820
  • 4
  • 21
  • 33
  • `std::function` is C++11 and OP is limited to C++03 – Marek R Jul 26 '19 at 11:38
  • also "you need std::function" is a bit too strong. `std::function` comes with quite some overhead that is actually much less often needed than it is used. Why not simply accept any callable without wrapping it into a `std::function` ? That would even work pre c++11 – 463035818_is_not_an_ai Jul 26 '19 at 11:39
  • @formerlyknownas_463035818 it would work pre C++11 only on MSVC. C++03 with tr1 provides: `std::tr1::function` and `std::tr1::bind`, but MS took a shortcut and immediately made it visible as a `std::function`. I think `tr1` is available in MSVC 2010 (as tagged by OP). – Marek R Jul 26 '19 at 11:48
  • @Marek R std::function is in msvc 2010. I use it for years in production without any problem. – Michael Aigner Jul 26 '19 at 12:35
  • @selfishness there are much better ways in c++ to do, I just post an equivalent solution for his c# code – Michael Aigner Jul 26 '19 at 12:38
  • @tonka Thankyou for your example. Actually `std::function` is not available in my C++ version. But it's a good hint for the way to go, if you know better ways can you edit your reply? I know C++ is powerful but I'm quite new on it full possibility. – Shyguy Jul 26 '19 at 13:11
  • @MarekR I meant it would work with pre C++11 when you dont use `std::function` – 463035818_is_not_an_ai Jul 26 '19 at 13:31
  • 1
    @Shyguy in msvc 2010 sp1 there is a std::function . If you use it without sp, then upgrade because there a really bad bug in msvc 2010 – Michael Aigner Jul 26 '19 at 13:34