0

Given the following base class:

struct ValueType {
    String
    ToString(String const& format) const;
};

I want this overload to be called for types deriving from ValueType:

String FormatValue(const ValueType& value, const String& format)
{
    return value.ToString(format);
}

Otherwise, I want this overload to be called:

template <typename T>
String FormatValue(const T& value, const String& format);

How can I ensure that derived types do not instead call the second overload?

The original question is located here.

3
  • Any reason at all that you don't just use the existing practice of IOstreams for all classes? And take my advice: Coding Java or C# in C++ is a friggin bad idea.
    – Xeo
    Mar 8, 2012 at 2:49
  • 1
    I seriously trimmed down your question to what I think is really the core of it. While it was originally well-written, I don't think all this information was very relevant to your problem. You can rollback to your original version if you think this is wrong.
    – Luc Danton
    Mar 8, 2012 at 3:02
  • The original question can still be viewed through the edit history by clicking on the time of the last edit, no need to save it elsewhere. :)
    – Xeo
    Mar 8, 2012 at 3:31

1 Answer 1

3

I am not too fond of what you are trying to do for different reasons (including the ValueType interface, why not use AnyToString always?), but at any rate you should be able to solve your problem with SFINAE

template <typename T>
typename enable_if< !is_base_of<ValueType, T>::value, String>::type
FormatValue( T const & value, const String& format ) { ... }

What that code does (once you make it compile :) is inhibiting the template function when the condition is met. When the compiler considers the template as an overload it will try to substitute the type, and the enable_if instantiation will fail to have a nested type if the condition is met, so the substitution fails and the template is discarded. After that, the best overload will be the version that takes a ValueType object.

9
  • @J.N.: This question has been here for 15 minutes. :P David answered 3 mins ago. Slowpoke!
    – Xeo
    Mar 8, 2012 at 3:01
  • You'll need to use boost in your answer, (I don't think there's an std::disable_if).
    – J.N.
    Mar 8, 2012 at 3:02
  • @Xeo and I'm typing on an iPad, how much more advantage do you need? Mar 8, 2012 at 3:08
  • @DavidR What I am trying to accomplish is to allow for custom formatting and padding syntax for example .. Format("Total is: {0:,$ #.##|-20}", amount) would convert amount into double into a thousand separated string having two decimal places and left padded to at least 20 characters. Any type which can define its own custom formatting arguments.
    – sysrpl
    Mar 8, 2012 at 3:08
  • 1
    The text after that still shows disable_if, so I fixed that. @sysrpl, here's the (my) standard link to learning SFINAE.
    – Xeo
    Mar 8, 2012 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.