When declaring a template you can choose either "class" or "typename" for a template parameter.
Example:
template<typename T> class vector { // ... };
or
The first Project Euler problem is to calculate the sum of all integers below 1000 which are divisible by either 3 or 5.
My solution is implemented entirely in C++ templates. The value is recursively calculated at compile time. The template specialization struct Problem1<0> stops the recursion and returns 0.
To compile this code with gcc you must expand the maximum allowed template recursion depth to at least 1000.
The boost::lexical_cast<> utility is a handy way of converting objects to and from strings, providing a mechanism that many scripting languages have built in. lexical_cast works with any C++ type that has ostream and istream operators defined for it, that is, any object that cout << object; or cin >> object; would work with.
This boost facility also does intelligent things such as throwing a bad_cast exception if the operation causes an error flag on the stream to be set. I use boost::lexical_cast<> throughout my code at work for things like serialization of objects for human readable communications.
It is also sprinkled liberally throughout my code for handling things like quick debug logging:
myLogger.log("Error occurred: " + boost::lexical_cast<std::string>(getErrorNo()));
However, it doesn't really make sense to pull boost into your project if you only need a handy tool for doing conversions to strings. It is simple to create your own version with a template:
Sometimes, in the course of C++ template based programming it might be desirable to have a constructor that is templated, like the following contrived exampled:
#include <iostream> struct TestClass { template<typename T> TestClass(const T &t) { std::cout << "Constructed a TestClass " << t << std::endl; } };
By creating a templated constructor, however, we have created an infinite number of automatic type conversions. That is, the following code does compile:
void TakeATestClass(const TestClass &t)
Release 0.0.2 of Swig Starter Kit was just released.
This release sees the addition of template usage examples, including custom function templates and STL usage.
Using a SWIG template declaration we are able to instantiate a specific template and use it from our script code.
//SWIG Code %include <std_string.i> %include <std_vector.i> %template(String_Vector) std::vector<std::string>;
We can now use the std container classes with our Lua code:
Pass by iterator is not a new concept, but one that we are going to perhaps give a new name to today and propose as a standard for normal usage.
If you take for example the normal way of passing around standard containers:
// Print a vector of strings separated by spaces. void print(const std::vector<std::string> &t_vector) { for(std::vector<std::string>::const_iterator itr = t_vector.begin(); itr != t_vector.end(); ++itr) { std::cout << *itr << " "; } } std::vector<std::string> m_vec; m_vec.push_back("Hello"); m_vec.push_back("World"); print(m_vec);
The idea of "Pass By Iterator" that we are proposing involves never again writing code like the above.
Recent comments
22 hours 48 min ago
2 days 5 hours ago
3 days 11 hours ago
3 days 20 hours ago
1 week 6 days ago
2 weeks 3 days ago
2 weeks 5 days ago
3 weeks 2 days ago
3 weeks 2 days ago
3 weeks 4 days ago