How to define a new function/struct/class if and only if it does not cause a redefinition error?

Viewed 63

I am generating c++ code and I need to define a new function/struct/class if and only if it does not cause a redefinition error. How can I achieve this? I think the solution would be to try and redefine the new function/struct/class and make it fail in a SFINAE-friendly way, but I have no clue on how to do this.

For example:

int foo (int x, int y) { 
   return x + y;
}

int foo (int z, int a) { // Obvious redefinition here, how can I make it fail in a SFINAE friendly way?
   return x * y;
} 

template <typename T, typename U>
struct bar {};

template <typename T, typename U> // Partial template specialisation does not actually specialise the parent template, how do I make this definition fail in a SFINAE friendly way?
struct bar<T, U> {}; 
0 Answers
Related