#include <iostream>
#include <type_traits>
template<class Typ>
class base {
public:
const base<Typ> operator+() const{
base<Typ> result;
result.fElement = +fElement;
return result;
}
template<typename T>
typename std::enable_if<std::is_same<T, base<double>>::value,
const base<double>>::type
operator^(const T& rgh) const
{
std::cout << fElement << " ^ " << rgh.fElement << '\n';
return *this;
}
private:
Typ fElement;
};
typedef base<double> derived;
int main(){
derived a, b;
+a; // It compiles
a^b; // It compiles
+a^b; // It compiles
+(a^b); // It compiles
base<int> c;
+c; // It compiles
// c^c; // no ^ for base<int>
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHlwZV90cmFpdHM+Cgp0ZW1wbGF0ZTxjbGFzcyBUeXA+CmNsYXNzIGJhc2UgewpwdWJsaWM6CiAgICBjb25zdCBiYXNlPFR5cD4gb3BlcmF0b3IrKCkgY29uc3R7CiAgICAgICAgYmFzZTxUeXA+IHJlc3VsdDsKICAgICAgICByZXN1bHQuZkVsZW1lbnQgPSArZkVsZW1lbnQ7CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgIH0KCiAgICB0ZW1wbGF0ZTx0eXBlbmFtZSBUPgogICAgdHlwZW5hbWUgc3RkOjplbmFibGVfaWY8c3RkOjppc19zYW1lPFQsIGJhc2U8ZG91YmxlPj46OnZhbHVlLAogICAgY29uc3QgYmFzZTxkb3VibGU+Pjo6dHlwZQogICAgb3BlcmF0b3JeKGNvbnN0IFQmIHJnaCkgY29uc3QKICAgIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgZkVsZW1lbnQgPDwgIiBeICIgPDwgcmdoLmZFbGVtZW50IDw8ICdcbic7CiAgICAgICAgcmV0dXJuICp0aGlzOwogICAgfQpwcml2YXRlOgogICAgVHlwIGZFbGVtZW50Owp9OwoKdHlwZWRlZiBiYXNlPGRvdWJsZT4gZGVyaXZlZDsKCmludCBtYWluKCl7CiAgICBkZXJpdmVkIGEsIGI7CiAgICArYTsgICAgIC8vIEl0IGNvbXBpbGVzCiAgICBhXmI7ICAgICAgICAvLyBJdCBjb21waWxlcwogICAgK2FeYjsgICAgICAgLy8gSXQgY29tcGlsZXMKICAgICsoYV5iKTsgICAgIC8vIEl0IGNvbXBpbGVzCgogICAgYmFzZTxpbnQ+IGM7CiAgICArYzsgLy8gSXQgY29tcGlsZXMKLy8gICAgIGNeYzsgLy8gbm8gXiBmb3IgYmFzZTxpbnQ+Cn0K