Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Code Snippet] Generic TryParse
#3
It's a templated function, that part is the declaration of the template types. In the above code, T is used as an alias for the type for the variable "out".

An example of using this function would be:
Code:
std::string str = "10";
int val = 0;
if(tryParse(str, val))
{
    // val should now be 10
    assert(val == 10);
}

Because the template type is used as a parameter, the compiler is smart enough to work out that in the above example, T is of type int. If the template type is not used as a parameter, you would have to explicitly specify it (think STL containers).
Code:
// int is used as the template type of the list
std::list<int> myList;

Strictly speaking the two examples aren't equivalent since std::list is a class, and you always have to specify class template types. The general principal between a templated class and a templated function (or even a templated member function, or even a templated memeber function in a templated class) are all basically the same.

See templates.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply


Messages In This Thread
[Code Snippet] Generic TryParse - by MrD. - 01-04-2010, 07:38 AM
RE: [Code Snippet] Generic TryParse - by Gaijin - 01-04-2010, 09:26 AM
RE: [Code Snippet] Generic TryParse - by MrD. - 01-04-2010, 10:22 AM
RE: [Code Snippet] Generic TryParse - by Gaijin - 01-04-2010, 10:28 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)