@louie999: Congrats on getting it working!
The things in the <> brackets are what get used to generate a real function from the template function.
The things in the () brackets are what get passed to the function after its been generated.
#include <iostream>
using namespace std;
template<typename Type, int FixedTemplatedValue>
void TemplatedFunc(Type value)
{
std::cout << "Fixed templated value: " << FixedTemplatedValue
<< "\tPassed in changable value: " << value << std::endl;
}
int main()
{
auto FuncSeventeen = &TemplatedFunc<std::string, 17>;
auto FuncTwoHundred = &TemplatedFunc<float, 200>;
//NOTE: These functions always print "17" as their fixed value,
//because it's now BUILT INTO the function.
//They also always take a std::string as their first parameter, because it's BUILT IN at compile time,
//but they let you decide at run time what the value of that string is.
FuncSeventeen("Meow");
FuncSeventeen("Purr");
//NOTE: These functions always print "200" as their fixed value,
//because it's now BUILT INTO the function.
//They also always take a float as their first parameter, because it's BUILT IN at compile time,
//but they let you decide at run time what the value of that float is.
FuncTwoHundred(0.257f);
FuncTwoHundred(1.5f);
return 0;
}
This prints:
Fixed templated value: 17 Passed in changable value: Meow
Fixed templated value: 17 Passed in changable value: Purr
Fixed templated value: 200 Passed in changable value: 0.257
Fixed templated value: 200 Passed in changable value: 1.5
Test it here
When you do:
TemplatedFunc<std::string, 17>("Meow");
It's the same as:
auto FuncSeventeen = &TemplatedFunc<std::string, 17>;
FuncSeventeen("Meow");
The compiler at compile-time creates the function "TemplatedFunc<std::string, 17>" from the template, by passing in the template arguments <std::string> and <17>.
Then, at run-time, the program calls the function, with the function arguments ("Meow").
@Servant, I completely forgot to declare args... D: and isn't std::make_unique only for C++14? I'm using C++11.
Yes, std::make_unique() was forgotten in C++11, so they added it in C++14.
However, you could just add it to your code yourself, and then remove it when you get access to the real thing.
//std::make_unique implementation (it was forgotten in the C++11 standard, and will be added later).
//Once it's added, I can just remove this function from here.
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}
Or better yet, you could just enable C++14 in your compiler, and start using those features. It doesn't break any of your existing code, and adds some cool features.
But if your current compiler doesn't support it, then I understand.