Dies ist etwas anders für C++; ich weiß nicht, ob es als Überladen durch Rückgabetyp direkt betrachtet werden würde. Es handelt sich eher um eine Template-Spezialisierung, die in der Art von.
util.h
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <sstream>
#include <algorithm>
class util {
public:
static int convertToInt( const std::string& str );
static unsigned convertToUnsigned( const std::string& str );
static float convertToFloat( const std::string& str );
static double convertToDouble( const std::string& str );
private:
util();
util( const util& c );
util& operator=( const util& c );
template<typename T>
static bool stringToValue( const std::string& str, T* pVal, unsigned numValues );
template<typename T>
static T getValue( const std::string& str, std::size_t& remainder );
};
#include "util.inl"
#endif UTIL_H
util.inl
template<typename T>
static bool util::stringToValue( const std::string& str, T* pValue, unsigned numValues ) {
int numCommas = std::count(str.begin(), str.end(), ',');
if (numCommas != numValues - 1) {
return false;
}
std::size_t remainder;
pValue[0] = getValue<T>(str, remainder);
if (numValues == 1) {
if (str.size() != remainder) {
return false;
}
}
else {
std::size_t offset = remainder;
if (str.at(offset) != ',') {
return false;
}
unsigned lastIdx = numValues - 1;
for (unsigned u = 1; u < numValues; ++u) {
pValue[u] = getValue<T>(str.substr(++offset), remainder);
offset += remainder;
if ((u < lastIdx && str.at(offset) != ',') ||
(u == lastIdx && offset != str.size()))
{
return false;
}
}
}
return true;
}
util.cpp
#include "util.h"
template<>
int util::getValue( const std::string& str, std::size_t& remainder ) {
return std::stoi( str, &remainder );
}
template<>
unsigned util::getValue( const std::string& str, std::size_t& remainder ) {
return std::stoul( str, &remainder );
}
template<>
float util::getValue( const std::string& str, std::size_t& remainder ) {
return std::stof( str, &remainder );
}
template<>
double util::getValue( const std::string& str, std::size_t& remainder ) {
return std::stod( str, &remainder );
}
int util::convertToInt( const std::string& str ) {
int i = 0;
if ( !stringToValue( str, &i, 1 ) ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to int";
throw strStream.str();
}
return i;
}
unsigned util::convertToUnsigned( const std::string& str ) {
unsigned u = 0;
if ( !stringToValue( str, &u, 1 ) ) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to unsigned";
throw strStream.str();
}
return u;
}
float util::convertToFloat(const std::string& str) {
float f = 0;
if (!stringToValue(str, &f, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to float";
throw strStream.str();
}
return f;
}
double util::convertToDouble(const std::string& str) {
float d = 0;
if (!stringToValue(str, &d, 1)) {
std::ostringstream strStream;
strStream << __FUNCTION__ << " Bad conversion of [" << str << "] to double";
throw strStream.str();
}
return d;
}
In diesem Beispiel wird nicht genau die Auflösung von Funktionsüberladungen nach Rückgabetyp verwendet, aber diese C++-Nichtobjektklasse verwendet die Vorlagenspezialisierung, um die Auflösung von Funktionsüberladungen nach Rückgabetyp mit einer privaten statischen Methode zu simulieren.
Jeder der convertToType
Funktionen rufen die Funktionsvorlage stringToValue()
und wenn Sie sich die Implementierungsdetails oder den Algorithmus dieser Funktionsvorlage ansehen, ruft sie getValue<T>( param, param )
und es gibt einen Typ zurück T
und Speicherung in einer T*
die in die Datei stringToValue()
Funktionsvorlage als einen ihrer Parameter.
Abgesehen von so etwas hat C++ nicht wirklich einen Mechanismus, um die Auflösung von Funktionsüberladungen nach Rückgabetyp zu ermöglichen. Möglicherweise gibt es andere Konstrukte oder Mechanismen, die mir nicht bekannt sind, die eine Auflösung nach Rückgabetyp simulieren könnten.
2 Stimmen
Mögliche Duplikate von Überladen einer C++-Funktion entsprechend dem Rückgabewert
0 Stimmen
@user195488 dies ist kein Duplikat, weil es eine allgemeine Frage ist.