Re: How to default an undefined operation?
Am 05.09.2012 23:53, schrieb Casey Carter:
On 2012-09-05 02:48, Ivan Godard wrote:
I have a template class taking a typename T argument, and need to
define a differenceType representing the abstract "distance" between
any pair of values. The template is instantiated with enums,
built-in types, and random user classes that define
operator-(T, T). This difference type was initially declared as:
typedef typeof(T() - T()) differenceType;
I recently converted an existing enum to a C++11 "enum class", and
the above broke because operator- was not defined for enum classes
and there is no conversion to something that defines it.
This is exactly the use case for std::declval<T>(): "creating" an
expression of type T for use in non-evaluated contexts - e.g.,
decltype(), sizeof() - without needing a valid constructor
expression. Try:
typedef
decltype(std::declval<T>() - std::declval<T>()) differenceType;
You have misunderstood the OP, I think. The problem he is stumbling
accross is not related to the expression T() versus your recommended
std::declval<T>(). Instead, the problem is that the expression
T() - T()
is not well-formed for T equal to a scoped enum type, because these
types have no operator - nor do they participate in promotion to
integral types. Using std::declval<T>() instead won't solve this problem.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]