EDIT:
Another solution ... if your IDE library partially supports C++11 , you can initialize std::vector during the call, i.e.
template <typename T> void TestFunction(std::vector<T> vect) { .... } .... TestFunction(std::vector<int>{1,2,3});
The advantages of this approach are that the STL automatically frees the allocated memory when the function goes out of scope.
If this does not work, you can resort to two liners ...
template <typename T> void TestFunction(std::vector<T> vect) { .... } .... std::vector<int> tmp(1,2,3); TestFunction(tmp);
The big drawback is that here the memory is on the stack until you leave this area (or explicitly resize the vector to zero length.
Both approaches have some advantages ... the counter is built-in, and you have access to other useful member functions or partner methods (e.g. std::sort ).
......................................
Why not use variable arguments? See the answer here, for example ...
Is it a good idea to use varargs in the C API to set a pair of key values?
In non-<100> compatible compilers (like your IDE) you can try ...
template <typename T> TestFunction(const unsigned int count, T * arr) TestFunction<std::string>(10, new string[] {"One", "Two", "Three"});
(It seems you cannot use this in your development environment, but ...) If you are sure that you work only on modern machines and mostly use simple types, this is the best / most standards compatible with the solution ...
In C++11 you can use std::initializer , which is located in std::vector :
#include<vector> template <typename T> void TestFunction(const std::initializer_list<T>& v) { } int main() { TestFunction<double>({1.0, 2.0}); return 0; }
..........................
... however, this requires your compiler to be C+11 , so it was not completely portable. For anything other than simple types, it also becomes harder to read.
I understand that you are talking on the function call, but you might want to rethink this because of the readability and ease of approach to coding.
I agree with part of your approach - you want to use the template function (this handles the type variable). Before you call, you initialize your collection of elements of the same type in the temporary standard C array or std::vector / std::list (shell of the STL array).
http://www.cplusplus.com/doc/tutorial/templates/
http://www.cplusplus.com/reference/vector/
http://www.cplusplus.com/reference/list/
This is more lines of code, but it is much more readable and standardized.
i.e.
Instead of...
MyFunction({1,2,3});
Using:
template <typename T> void TestFunction(const int count, T * arr) { for (unsigned int i = 0; i < count; i++) { .... arr[i] ... ;
... or...
#include <vector> template <typename T> void TestFunction(std::vector<T> vect) { for (unsigned int i = 0; i < vect.size(); i++) { .... vect[i] ... ; //do stuff ... } } int main() { std::vector<int> myVect; myVect.push_back(1); myVect.push_back(2); myVect.push_back(3); TestFuntion<int>(myVect); }
std::list would also be quite acceptable and might work better, depending on your use case.