A reference taking function with a default value - c++

Consider this function:
void func(int* a = nullptr)
{
if (a)
*a = 1;
}
I can then call function without the parameter, as it has the default value (func()) or I can obtain a value via the parameter (func(somePtr)).
Now I was trying to figure out either the same functionality is possible to accomplish without method overloading with references. The closest thing I got was utilizing an universal reference, but in this case I am bound to using template types and in my case my type is strict. Is there a way to translate this bare pointer functionality to references functionality?
One way of doing this I came up with is:
void func(int& a)
{
a = 5;
}
void func()
{
int dummy;
fund(dummy);
}
I am aiming for something like this (wont work):
template<typename T>
void func(T&& a = int())
{
a = 1;
}

Overloading is the most natural way, e.g.
void func(int& a) { a = 1; /* assume some other complex logic here */ }
void func(int&& a = int()) { func(a); }
It is possible to make the SFINAE version work, although I'm sure aesthetic views will differ:
#include <type_traits>
template<typename T, typename U>
using EnableIfSame = std::enable_if_t<std::is_same_v<std::remove_reference_t<T>, U>>;
template<typename T = int, typename = EnableIfSame<T, int>>
void func(T&& a = int())
{
a = 1;
}
If you don't have C++17 then is_same_v<Z> would need to be replaced with is_same<Z>::value, and if not C++14 then enable_if_t<Z> with typename enable_if<Z>::type .

I'm not sure I could figure out your problem. Maybe this method answers your question.
void *p;
template <typename T>
void func(T def, T& t = *(T *)p)
{
if (&t != p)
t = def;
}
int main()
{
int a;
func(1);
func(1, a);
}

Related

C++ varadic template function - only one instance of known parameter pack

I want to use a fold expression but the function will only ever be used with one known parameter pack.
i.e.
template <class... Types>
Foo fn_impl(Arg arg) {
// here, a fold expression involving Types... that returns a Foo
}
Foo fn(Arg arg) {
return fn_impl<Bar, Baz>(arg);
}
And that's it, fn_impl won't ever be used again.
Is there a way to make this less verbose?
Ideally, I'd like to write the implementation of fn in its body, without a separate implementation function (which adds noise, imo).
I know I could "unroll" the fold expression with the types in the parameter pack by hand, but in this case, using a fold expression is just very convenient to make the implementation of fn not too verbose.
Here's a complete example, see highlighted QUESTION comment:
#include <cassert>
#include <cstdio>
#include <memory>
struct Base {
virtual ~Base() {}
virtual const char *get_name() const = 0;
};
template <class Derived> struct Base_CRTP : public Base {
const char *get_name() const final {
return static_cast<const Derived *>(this)->name;
}
};
struct A : Base_CRTP<A> {
static constexpr const char *name = "A";
};
struct B : Base_CRTP<B> {
static constexpr const char *name = "B";
};
#define ITest_DERIVED_CLASSES A, B
// QUESTION: Can this be entirely moved into the definition of #1?
template <class IType, class... Types>
std::unique_ptr<IType> make_by_class_index___impl(int class_index) {
int i = 0;
std::unique_ptr<IType> ret;
([&] {
if (i++ == class_index)
ret = std::make_unique<Types>();
return ret != nullptr;
}() ||
...);
return ret;
}
// #1
std::unique_ptr<Base> make_by_class_index(int class_index) {
return make_by_class_index___impl<Base, ITest_DERIVED_CLASSES>(class_index);
}
template <class... Types> void print_pack_names() { (puts(Types::name), ...); }
int main() {
print_pack_names<ITest_DERIVED_CLASSES>();
puts("");
auto p = make_by_class_index(0);
assert(p != nullptr);
printf("p name: %s\n", p->get_name());
auto p2 = make_by_class_index(1);
assert(p2 != nullptr);
printf("p2 name: %s\n", p2->get_name());
auto p3 = make_by_class_index(99);
assert(p3 == nullptr);
}
In lack of sufficient details, let's assume without loss of generality that Arg is int that that Foo, Bar and Baz are defined as follows:
struct Foo { int x; };
struct Bar { static constexpr int foo() { return 1; } };
struct Baz { static constexpr int foo() { return 2; } };
If you may use C++20 you can migrate a) a variadic function which contains a fold expression and b) a call to site to it, e.g.:
template <typename... Types> Foo fn_impl(int arg) {
return { arg + (Types::foo() + ...) };
}
// at call site
fn_impl<Bar, Baz>(arg);
into a single generic immediately invoked lambda, leveraging that P0428R2 (introduced in C++20) allows template heads for generic lambdas:
Foo fn(int arg) {
return []<typename... Types>(int arg) -> Foo {
return { arg + (Types::foo() + ...) };
}
.operator()<Bar, Baz>(arg);
}
This arguably looks quite complex, though, particularly as you need to use the operator name syntax to provide explicit template arguments for the generic lambdas. The separate function approach is arguably easier to follow for future maintainers.

Explicit void pointer as function parameter

I have a function:
int foo(void * ptr)
{
// ...
}
Can I syntactically (not with compiler warnings, etc.) in C++11/14 disable passing there pointers other than void * itself?
For example, now it can be called like:
foo(new int(42));
I need to disable this.
I suppose there are many other ways of doing it.
Using template functions is simple (it works with C++98 too)
template <typename X>
int foo (X * ptr);
int foo (void * ptr)
{ return 1; }
int main()
{
int i;
void * vp = &i;
foo(vp); // OK
foo(&i); // linker error
return 0;
}
As pointed by frymode, the preceding solution give a linker error, not a compiler error, and it's better to get a compiler error.
Using delete (from C++11) we can get a compiler error by using this instead:
template <typename X>
int foo (X ptr) = delete;
Hope this helps.
You can make use of pedantic pointer idiom. Your code should look as bellow. It makes use of the fact that there is no implicit conversions at the higher that one level of indirection:
[live]
int foo_impl(void * ptr, void **)
{
return 0;
}
template <typename T>
void foo(T* t)
{
foo_impl(t, &t);
}
int main()
{
void* pv;
foo(pv);
//foo(new int(2)); // error: error: invalid conversion from 'int**' to 'void**'
}
If you want exact type match you can use std::enable_if with std::is_same
#include <iostream>
#include <type_traits>
template <typename T,
typename = typename std::enable_if_t<std::is_same<T, void*>::value>>
int foo(T value)
{
return 5;
}
int main()
{
// return foo(new int(42)); // error: no matching function for call to 'foo(int*)'
return foo((void*)(new int(42)));
}
You can turn the function to a template one, then use a static_assert and std::is_void from type_traits:
template<typename T>
int foo(T *ptr) {
static_assert(std::is_void<T>::value, "!");
// ....
}
Otherwise, you can use a std::enable_if_t on the return type:
template<typename T>
std::enable_if_t<std::is_void<T>::value, int>
foo(T *ptr) {
// ....
return 0;
}
And so on, other interesting solutions have already been proposed by other users with their answers.
Here is a minimal, working example:
#include<type_traits>
template<typename T>
int foo(T *ptr) {
static_assert(std::is_void<T>::value, "!");
// ....
return 0;
}
int main() {
int i = 42;
void *p = &i;
foo(p);
// foo(&i); // compile error
}
The idiomatic way would be to create a new type to represent void* to avoid the problem you are describing. Many advocates of good C++ practices suggest creating types to avoid any doubt about what should be passed in and also avoid the compiler letting you.
class MyVoid
{
//... implement in a way that makes your life easy to do whatever you are trying to do with your void* stuff
};
int foo(MyVoid ptr)
{
// ...
}
You don't need C++11 to ensure a compile-time error:
template<class> struct check_void;
template<> struct check_void<void> { typedef void type; };
template<class T> typename check_void<T>::type *foo(T *ptr) { return ptr; }
int main()
{
foo(static_cast<void *>(0)); // success
foo(static_cast<int *>(0)); // failure
}

Template specialization type lists without macros?

So given a function f is there way to define specific behaviors for certain individual non related classes, without some macro-foo
e.g. a replacement / way of accomplishing the same thing as :
//p for param
template<typename T>
T f(T p){ // some default op};
template<>
T f<float>(T p)
{ return 2*p; }
template<>
T f<double>(T p)
{ return 2*p; }
template<>
T f<int>(T p)
{ return 2*p; }
template<>
T f<std::string>(T p)
{ //return p copies of the string appended together; }
template<>
T f<std::vector>(T p)
{ //return the vector's element's copied}
// etc
No I would not like normal overloads.
ideally something like
template
if T in [int, float, double]
T f(T p) { return 2*p; }
else // define a default other behavior. which you could do in python.
Anyway to make a decision based on the class of T? A possible solution I can think of which is very ... not pretty would be using typeid and demangling.
say for some reason you had a super generic function and some 15 different classes, writing that all using overloading would not be pretty.
If I understand your question correctly you want to make a list of types which should be handled differently than all other types?
If that is the case make a list of the desired types and use a meta function to determine if T is in your list of types returning true or false.
then use enable_if to switch between function implementations.
#include <boost\mpl\vector.hpp>
#include <boost\mpl\contains.hpp>
typedef boost::mpl::vector<char,int,unsigned,long,unsigned long> types;
template<class T>
typename std::enable_if<boost::mpl::contains<types,T>::value, T>::type
foo(T t)
{
std::cout << "special foo\n";
return t;
}
template<class T>
typename std::enable_if<boost::mpl::not_<boost::mpl::contains<types,T>>::type::value, T>::type
foo(T t)
{
std::cout << "normal foo\n";
return t;
}
void main()
{
foo(1); //calls special foo because int is in the list
foo(1.1); //calls normal foo because float is not in the list
}
Update: boost.MPL is outdated, if you have C++11 support use brigand
It seems to me that you need rather normal (non template) function overloads:
float f(float p)
{ return 2*p; }
double f(double p)
{ return 2*p; }
int f(int p)
{ return 2*p; }
std::string f(std::string p)
{ //return p copies of the string appended together; }
std::vector f(std::vector p)
{ //return the vector's element's copied}

How can I iterate over a packed variadic template argument list?

I'm trying to find a method to iterate over an a pack variadic template argument list.
Now as with all iterations, you need some sort of method of knowing how many arguments are in the packed list, and more importantly how to individually get data from a packed argument list.
The general idea is to iterate over the list, store all data of type int into a vector, store all data of type char* into a vector, and store all data of type float, into a vector. During this process there also needs to be a seperate vector that stores individual chars of what order the arguments went in. As an example, when you push_back(a_float), you're also doing a push_back('f') which is simply storing an individual char to know the order of the data. I could also use a std::string here and simply use +=. The vector was just used as an example.
Now the way the thing is designed is the function itself is constructed using a macro, despite the evil intentions, it's required, as this is an experiment. So it's literally impossible to use a recursive call, since the actual implementation that will house all this will be expanded at compile time; and you cannot recruse a macro.
Despite all possible attempts, I'm still stuck at figuring out how to actually do this. So instead I'm using a more convoluted method that involves constructing a type, and passing that type into the varadic template, expanding it inside a vector and then simply iterating that. However I do not want to have to call the function like:
foo(arg(1), arg(2.0f), arg("three");
So the real question is how can I do without such? To give you guys a better understanding of what the code is actually doing, I've pasted the optimistic approach that I'm currently using.
struct any {
void do_i(int e) { INT = e; }
void do_f(float e) { FLOAT = e; }
void do_s(char* e) { STRING = e; }
int INT;
float FLOAT;
char *STRING;
};
template<typename T> struct get { T operator()(const any& t) { return T(); } };
template<> struct get<int> { int operator()(const any& t) { return t.INT; } };
template<> struct get<float> { float operator()(const any& t) { return t.FLOAT; } };
template<> struct get<char*> { char* operator()(const any& t) { return t.STRING; } };
#define def(name) \
template<typename... T> \
auto name (T... argv) -> any { \
std::initializer_list<any> argin = { argv... }; \
std::vector<any> args = argin;
#define get(name,T) get<T>()(args[name])
#define end }
any arg(int a) { any arg; arg.INT = a; return arg; }
any arg(float f) { any arg; arg.FLOAT = f; return arg; }
any arg(char* s) { any arg; arg.STRING = s; return arg; }
I know this is nasty, however it's a pure experiment, and will not be used in production code. It's purely an idea. It could probably be done a better way. But an example of how you would use this system:
def(foo)
int data = get(0, int);
std::cout << data << std::endl;
end
looks a lot like python. it works too, but the only problem is how you call this function.
Heres a quick example:
foo(arg(1000));
I'm required to construct a new any type, which is highly aesthetic, but thats not to say those macros are not either. Aside the point, I just want to the option of doing:
foo(1000);
I know it can be done, I just need some sort of iteration method, or more importantly some std::get method for packed variadic template argument lists. Which I'm sure can be done.
Also to note, I'm well aware that this is not exactly type friendly, as I'm only supporting int,float,char* and thats okay with me. I'm not requiring anything else, and I'll add checks to use type_traits to validate that the arguments passed are indeed the correct ones to produce a compile time error if data is incorrect. This is purely not an issue. I also don't need support for anything other then these POD types.
It would be highly apprecaited if I could get some constructive help, opposed to arguments about my purely illogical and stupid use of macros and POD only types. I'm well aware of how fragile and broken the code is. This is merley an experiment, and I can later rectify issues with non-POD data, and make it more type-safe and useable.
Thanks for your undertstanding, and I'm looking forward to help.
If your inputs are all of the same type, see OMGtechy's great answer.
For mixed-types we can use fold expressions (introduced in c++17) with a callable (in this case, a lambda):
#include <iostream>
template <class ... Ts>
void Foo (Ts && ... inputs)
{
int i = 0;
([&]
{
// Do things in your "loop" lambda
++i;
std::cout << "input " << i << " = " << inputs << std::endl;
} (), ...);
}
int main ()
{
Foo(2, 3, 4u, (int64_t) 9, 'a', 2.3);
}
Live demo
(Thanks to glades for pointing out in the comments that I didn't need to explicitly pass inputs to the lambda. This made it a lot neater.)
If you need return/breaks in your loop, here are some workarounds:
Demo using try/throw. Note that throws can cause tremendous slow down of this function; so only use this option if speed isn't important, or the break/returns are genuinely exceptional.
Demo using variable/if switches.
These latter answers are honestly a code smell, but shows it's general-purpose.
If you want to wrap arguments to any, you can use the following setup. I also made the any class a bit more usable, although it isn't technically an any class.
#include <vector>
#include <iostream>
struct any {
enum type {Int, Float, String};
any(int e) { m_data.INT = e; m_type = Int;}
any(float e) { m_data.FLOAT = e; m_type = Float;}
any(char* e) { m_data.STRING = e; m_type = String;}
type get_type() const { return m_type; }
int get_int() const { return m_data.INT; }
float get_float() const { return m_data.FLOAT; }
char* get_string() const { return m_data.STRING; }
private:
type m_type;
union {
int INT;
float FLOAT;
char *STRING;
} m_data;
};
template <class ...Args>
void foo_imp(const Args&... args)
{
std::vector<any> vec = {args...};
for (unsigned i = 0; i < vec.size(); ++i) {
switch (vec[i].get_type()) {
case any::Int: std::cout << vec[i].get_int() << '\n'; break;
case any::Float: std::cout << vec[i].get_float() << '\n'; break;
case any::String: std::cout << vec[i].get_string() << '\n'; break;
}
}
}
template <class ...Args>
void foo(Args... args)
{
foo_imp(any(args)...); //pass each arg to any constructor, and call foo_imp with resulting any objects
}
int main()
{
char s[] = "Hello";
foo(1, 3.4f, s);
}
It is however possible to write functions to access the nth argument in a variadic template function and to apply a function to each argument, which might be a better way of doing whatever you want to achieve.
Range based for loops are wonderful:
#include <iostream>
#include <any>
template <typename... Things>
void printVariadic(Things... things) {
for(const auto p : {things...}) {
std::cout << p.type().name() << std::endl;
}
}
int main() {
printVariadic(std::any(42), std::any('?'), std::any("C++"));
}
For me, this produces the output:
i
c
PKc
Here's an example without std::any, which might be easier to understand for those not familiar with std::type_info:
#include <iostream>
template <typename... Things>
void printVariadic(Things... things) {
for(const auto p : {things...}) {
std::cout << p << std::endl;
}
}
int main() {
printVariadic(1, 2, 3);
}
As you might expect, this produces:
1
2
3
You can create a container of it by initializing it with your parameter pack between {}. As long as the type of params... is homogeneous or at least convertable to the element type of your container, it will work. (tested with g++ 4.6.1)
#include <array>
template <class... Params>
void f(Params... params) {
std::array<int, sizeof...(params)> list = {params...};
}
This is not how one would typically use Variadic templates, not at all.
Iterations over a variadic pack is not possible, as per the language rules, so you need to turn toward recursion.
class Stock
{
public:
bool isInt(size_t i) { return _indexes.at(i).first == Int; }
int getInt(size_t i) { assert(isInt(i)); return _ints.at(_indexes.at(i).second); }
// push (a)
template <typename... Args>
void push(int i, Args... args) {
_indexes.push_back(std::make_pair(Int, _ints.size()));
_ints.push_back(i);
this->push(args...);
}
// push (b)
template <typename... Args>
void push(float f, Args... args) {
_indexes.push_back(std::make_pair(Float, _floats.size()));
_floats.push_back(f);
this->push(args...);
}
private:
// push (c)
void push() {}
enum Type { Int, Float; };
typedef size_t Index;
std::vector<std::pair<Type,Index>> _indexes;
std::vector<int> _ints;
std::vector<float> _floats;
};
Example (in action), suppose we have Stock stock;:
stock.push(1, 3.2f, 4, 5, 4.2f); is resolved to (a) as the first argument is an int
this->push(args...) is expanded to this->push(3.2f, 4, 5, 4.2f);, which is resolved to (b) as the first argument is a float
this->push(args...) is expanded to this->push(4, 5, 4.2f);, which is resolved to (a) as the first argument is an int
this->push(args...) is expanded to this->push(5, 4.2f);, which is resolved to (a) as the first argument is an int
this->push(args...) is expanded to this->push(4.2f);, which is resolved to (b) as the first argument is a float
this->push(args...) is expanded to this->push();, which is resolved to (c) as there is no argument, thus ending the recursion
Thus:
Adding another type to handle is as simple as adding another overload, changing the first type (for example, std::string const&)
If a completely different type is passed (say Foo), then no overload can be selected, resulting in a compile-time error.
One caveat: Automatic conversion means a double would select overload (b) and a short would select overload (a). If this is not desired, then SFINAE need be introduced which makes the method slightly more complicated (well, their signatures at least), example:
template <typename T, typename... Args>
typename std::enable_if<is_int<T>::value>::type push(T i, Args... args);
Where is_int would be something like:
template <typename T> struct is_int { static bool constexpr value = false; };
template <> struct is_int<int> { static bool constexpr value = true; };
Another alternative, though, would be to consider a variant type. For example:
typedef boost::variant<int, float, std::string> Variant;
It exists already, with all utilities, it can be stored in a vector, copied, etc... and seems really much like what you need, even though it does not use Variadic Templates.
There is no specific feature for it right now but there are some workarounds you can use.
Using initialization list
One workaround uses the fact, that subexpressions of initialization lists are evaluated in order. int a[] = {get1(), get2()} will execute get1 before executing get2. Maybe fold expressions will come handy for similar techniques in the future. To call do() on every argument, you can do something like this:
template <class... Args>
void doSomething(Args... args) {
int x[] = {args.do()...};
}
However, this will only work when do() is returning an int. You can use the comma operator to support operations which do not return a proper value.
template <class... Args>
void doSomething(Args... args) {
int x[] = {(args.do(), 0)...};
}
To do more complex things, you can put them in another function:
template <class Arg>
void process(Arg arg, int &someOtherData) {
// You can do something with arg here.
}
template <class... Args>
void doSomething(Args... args) {
int someOtherData;
int x[] = {(process(args, someOtherData), 0)...};
}
Note that with generic lambdas (C++14), you can define a function to do this boilerplate for you.
template <class F, class... Args>
void do_for(F f, Args... args) {
int x[] = {(f(args), 0)...};
}
template <class... Args>
void doSomething(Args... args) {
do_for([&](auto arg) {
// You can do something with arg here.
}, args...);
}
Using recursion
Another possibility is to use recursion. Here is a small example that defines a similar function do_for as above.
template <class F, class First, class... Rest>
void do_for(F f, First first, Rest... rest) {
f(first);
do_for(f, rest...);
}
template <class F>
void do_for(F f) {
// Parameter pack is empty.
}
template <class... Args>
void doSomething(Args... args) {
do_for([&](auto arg) {
// You can do something with arg here.
}, args...);
}
You can't iterate, but you can recurse over the list. Check the printf() example on wikipedia: http://en.wikipedia.org/wiki/C++0x#Variadic_templates
You can use multiple variadic templates, this is a bit messy, but it works and is easy to understand.
You simply have a function with the variadic template like so:
template <typename ...ArgsType >
void function(ArgsType... Args){
helperFunction(Args...);
}
And a helper function like so:
void helperFunction() {}
template <typename T, typename ...ArgsType >
void helperFunction(T t, ArgsType... Args) {
//do what you want with t
function(Args...);
}
Now when you call "function" the "helperFunction" will be called and isolate the first passed parameter from the rest, this variable can b used to call another function (or something). Then "function" will be called again and again until there are no more variables left. Note you might have to declare helperClass before "function".
The final code will look like this:
void helperFunction();
template <typename T, typename ...ArgsType >
void helperFunction(T t, ArgsType... Args);
template <typename ...ArgsType >
void function(ArgsType... Args){
helperFunction(Args...);
}
void helperFunction() {}
template <typename T, typename ...ArgsType >
void helperFunction(T t, ArgsType... Args) {
//do what you want with t
function(Args...);
}
The code is not tested.
#include <iostream>
template <typename Fun>
void iteratePack(const Fun&) {}
template <typename Fun, typename Arg, typename ... Args>
void iteratePack(const Fun &fun, Arg &&arg, Args&& ... args)
{
fun(std::forward<Arg>(arg));
iteratePack(fun, std::forward<Args>(args)...);
}
template <typename ... Args>
void test(const Args& ... args)
{
iteratePack([&](auto &arg)
{
std::cout << arg << std::endl;
},
args...);
}
int main()
{
test(20, "hello", 40);
return 0;
}
Output:
20
hello
40

Can we create an IF statement that determines if a variable has been declared as a pointer in C/C++

Suppose a variable pa is always declared as one of two ways:
double * pa
OR
double pa
Can we create an IF statement that does the following
IF (pa is a pointer)
{ pa[0] = 1
}
ELSE
{ pa = 1}
EDIT:
Please see How to find out if a pointer array has been filled in C++/C for the follow up question
The basics of how C++0x std::is_pointer might work:
template <typename T>
struct is_pointer_helper {
static const bool value = false;
};
template <template T>
struct is_pointer_helper<T*> {
static const bool value = true;
};
For this example we don't want to have to use the type, so we need some template argument deduction:
template <typename T>
bool is_pointer(const T &) {
return is_pointer_helper<T>::value;
}
And we're done:
if (is_pointer(pa)) {
pa[0] = 1;
} else {
pa = 1;
}
BUT, note that this code still won't compile unless pa[0] = 1 and pa = 1 are both valid expressions (and for neither double nor double* are they both valid - if you were setting to 0 then both would be valid for double*, but still only the second for double). So probably what you want is not an explicit "if" test, but an overloaded function:
template <typename T>
void set_thingy(T &t) {
t = 1;
}
template <typename T>
void set_thingy(T *pt) {
set_thingy(*pt);
}
set_thingy(pa);
Or, since pa is always double or double*, there's no need to focus on the fact that one is a pointer and the other is not, they're just two different types that we need to treat differently. So no need for templates:
void set_thingy(double &d) { d = 1; }
void set_thingy(double *p) { *p = 1; }
set_thingy(pa);
Not directly, no. Suppose there were a way to do this, something like
if (__IS_POINTER(pa) {
pa[0] = 1;
}
else {
a = 1;
}
What happens if pa isn't a pointer? You still have that pa[0] = 1; statement in your program, and it's illegal, so the compiler is going to reject your program.
You might in principle be able to do a compile-time test:
#if __pa_IS_A_POINTER
pa[0] = 1;
#else
a = 1
#endif
but the power of the C++ preprocessor is very limited; it has no way to test the type of a variable or expression.
If you had this capability, what would you do with it? Tell us what your actual goal is.
In C++0x you can, using type traits and decltype:
#include <type_traits>
if (std::is_pointer<decltype(x)>::value)
{
/* ... */
}
In C++98/03 you don't have decltype, but now you'd have to explain how you could possibly have a variable without knowing its type. If you do have its type, you can use the same type trait:
#include <tr1/type_traits>
template <typename T> void bogus(T x) // T could be a pointer or not
{
if (std::tr1::is_pointer<T>::value) { /*...*/ }
}
You can also just define your own trait class if you don't have or want to use TR1:
template <typename> struct is_pointer { static const bool value = false; };
template <typename T> struct is_pointer<T*> { static const bool value = true; };