Is there a std function that returns only its parameter?

This should be trivial to implement, something like

template<typename T>
T & as_is(T & t) { return t; }

However, I would not want to write it (:

I did not find such a thing at www.cplusplus.com .

For those who ask, “what are you trying to do,” here’s what. I have a class that builds ascii tables, with a nice addition and that’s all. I will spare the details. The important thing is that it stores the strings (so that it can calculate how much for the pad). I want to implement a sort function and be able to tell the class to use the column as a specific type. If I want to sort by column ints (which, again, are inner rows), I would go through atoi. If sorting strings, I want to pass as_isor the equivalent of stl, if any.

+5
source share
2 answers

You cannot pass a template as if it were a function, so neither std :: forward nor your hypothetical nonstd :: identity function will work as is (as it were). You need to explicitly specify the target conversion type:

table.SortColumnUsing(3, nonstd::identity<std::string>);

This seems a little ugly to me because the type in the template specialization is a feature of the internal implementation of the table, and has nothing to do with the type that I expect from serializing the table for sorting purposes. Or maybe you are not storing the columns as std :: string.

atoi ? atoi char *, std::string, , int(const char*) int(const std::string&). , , int(const std::string&), Banana(const std string&). , , , , ++, .

, , . , , bool(const std::string&, const std::string&). , . (, : , /, ). ; std::string std::less<std::string> ( .)

, ... .

+2

?

Btw, MSVC, identity, operator(), . std::identity<std::string>(), , identity.

+3

All Articles