I have an interesting case of trying to have branch within a template function where the path is dependent on the interface implemented by the template type. This branch then determines the constructor of the return value. I am unsure if this type of branching is possible. The alternative it to split the function into two different function and have the user call the one that corresponds to the desired branch.
My question is twofold:
How can I perform the if statement based on the interface implementation?
How can I get the function to compile when the interface is not implemented? For example int
does not have a constructor with two parameters.
template<typename T>
T GetObject()
{
// If T implements interface, call interface constructor
if(typeid(T) implements inteface IInterface) // How can this be done?
return T(1, 2);
// If T does not implement the interface, call parameterless constructor
else
return T();
}
As you can easily deduce, your code as is has no way of working (even though I wish it did) because regardless of what you put in your if statements, every line has to be compiled - even if some will later be removed during optimization. Even if you had the correct line for:
The second line would still be compiled for
int
, which is no good.What you want is SFINAE: Substitution Failure Is Not An Error. Basically, you intentionally write a declaration that cannot compile but as long as there's an available overload that will compile, that's good enough! So to that end, we just take advantage of some type traits and the magic, yet incredibly simple, enable_if:
Although given Herb Sutter's article on function specialization, probably better to use overloading: