Code: Select all
template
class profile_integer;
< /code>
, für die ich einen Multiplikationsoperator auf < /p>
definieren möchte friend constexpr profile_integer operator*(const profile_integer& lhs, const profile_integer& rhs);
< /code>
Bisher gut, aber jetzt möchte ich in der Lage sein, die Multiplikation zwischen verschiedenen Typen zu verarbeiten, und da dies eine Vorlage ist, kann ich keine impliziten Conversions durchführen. Also gut, ich kann einen anderen Bediener (mit std :: complex
definieren.
Code: Select all
friend constexpr typename std::enable_if::value, profile_integer>::type operator*(const V& lhs, const profile_integer& rhs);
< /code>
Entschuldigung für den Rückgabetyp; Da v
Code: Select all
template
constexpr profile_integer operator*(const profile_integer& lhs, const profile_integer& rhs)
{
profile_integer x;
x.value = lhs.value * rhs.value;
return x;
}
template
constexpr typename std::enable_if, V>::value, profile_integer>::type operator*(const V& lhs, const profile_integer& rhs)
{
profile_integer x;
x.value = rhs.value * lhs;
return x;
}
< /code>
Aber jetzt habe ich ein Problem, für meine zweite Freundesklaration habe ich den Fehler < /p>
inline function 'operator*' is not defined
Code: Select all
function template partial specialization is not allowed
< /code>
Ich habe hier eine vollständige Kopie eines minimalen Beispiels bei Compiler Explorer. Ich kann dieses [url=viewtopic.php?t=15738]Problem[/url] beheben, indem ich die Überladungen des Freundes Operator in der Klassendefinition definiere. Ich frage mich jedoch, ob es eine Möglichkeit gibt, dies zu lösen, ohne dies zu tun, da ich gerne verstehen möchte, was hier vor sich geht.#include
#include
template
class profile_integer;
template
constexpr profile_integer operator*(const profile_integer& lhs, const profile_integer& rhs);
template
constexpr typename std::enable_if, V>::value, profile_integer>::type operator*(const V& lhs, const profile_integer& rhs);
template
class profile_integer
{
private:
T value;
public:
profile_integer() : value{0} {}
template
constexpr profile_integer(V x) : value{x} {}
friend constexpr profile_integer operator*(const profile_integer& lhs, const profile_integer& rhs);
template
friend constexpr typename std::enable_if, V>::value, profile_integer>::type operator*(const V& lhs, const profile_integer& rhs);
};
template
constexpr profile_integer operator*(const profile_integer& lhs, const profile_integer& rhs)
{
profile_integer x;
x.value = lhs.value * rhs.value;
return x;
}
template
constexpr typename std::enable_if, V>::value, profile_integer>::type operator*(const V& lhs, const profile_integer& rhs)
{
profile_integer x;
x.value = rhs.value * lhs;
return x;
}
int main()
{
profile_integer x;
x = x * x;
x = 2*x;
return 0;
}
Code: Select all
template
profile_integer mul(const V& lhs, const profile_integer& rhs)
{
return lhs * rhs.get_value();
}
< /code>
Ich kann diese Funktion aufrufen < /p>
int main()
{
profile_integer x;
x = x * x;
x = mul(2, x);
return 0;
}
< /code>
Und dies funktioniert < /p>
Wenn ich die Funktion einfach zu einem Freund in der Klassenerklärung erkläre < /p>
template
friend profile_integer mul(const V& lhs, const profile_integer& rhs);