Wie deklinge ich eine Überlastung einer Vorlagenfreund -Operatorin für eine Vorlagenklasse richtig, ohne sie inline zu dC++

Programme in C++. Entwicklerforum
Anonymous
 Wie deklinge ich eine Überlastung einer Vorlagenfreund -Operatorin für eine Vorlagenklasse richtig, ohne sie inline zu d

Post by Anonymous »

Ich arbeite in C ++ 20, aber ohne Konzepte. Ich habe einen Ganzzahl -Typ < /p>

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 
als Beispiel)
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 
sein könnte, brauchte ich dies.

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
Für die erste Deklaration konnte ich dies beheben, indem ich den Freund des Freundes deklarierte.

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;
}
Bearbeiten: Der C ++ 20 Standard 13.9.2 (c) stellt fest, dass Contexpr möglicherweise nicht mit explizitem Vorlagenspezialisierung verwendet wird. Durch das Entfernen des Contexpr kann der Code kompilieren, aber das Verknüpfen fällt immer noch fehl, wenn der Bediener*(...) den nicht enthält. Die Verwendung von führt weiterhin zu einem partiellen Vorlagenspezialisierungsfehler.

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);
Dies kompiliert, aber nicht verknüpft.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post