In this article we are going to introduce the concept of C++ Template Partial Specialization. This is meant to be just a primer on the topic and not exhaustive. The examples here will be used and referenced in later articles. A series of discussions about C++11, now that the language has been finalized, will be coming shortly.
In C++ a template class such as this:
template<typename LHS, typename RHS> struct Add_With_Magic { static int go(const LHS &t_lhs, const RHS &t_rhs) {
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:
Recent comments
2 days 11 hours ago
2 days 11 hours ago
3 days 7 hours ago
1 week 1 day ago
1 week 1 day ago
2 weeks 22 hours ago
9 weeks 3 days ago
13 weeks 4 days ago
16 weeks 2 days ago
16 weeks 4 days ago