Victor Bazarov wrote:
Philipp wrote:
>Hello I have a function writeValueXml(,,int) (see below) which
converts an int value to a string which is then passed to a
overloaded version of itself for printing writeValueXml(,,string)
This works for int. Is there a way to write one unique such a function
for all integer types? (signed/unsigned short/int/long) without
rewriting the code?
Maybe using the automatic promotion from one type to another? (what's
the promotion between signed and unsigned?)
Or with a template? (how to specify only int types are allowed?)
Thanks for answers
Phil
--- code ---
void Parameters::writeValueXml(TiXmlElement* atomElem, string
xmlElemName, int value){
std::stringstream converter;
converter << value;
string v = converter.str();
writeValueXml(atomElem, xmlElemName, v);
}
Why not write a template and let the compiler worry about resolving
the signed/unsigned issue?
class Parameters {
...
template<class Vvoid writeValueXml(TiXmlElement* atomElem,
string const& xmlElemName, V value) {
std::stringstream converter;
converter << value;
string v = converter.str();
writeValueXml(atomElem, xmlElemName, v);
}
...
};
Thank you for your answer. I have some problems to make it work. :-)
First a small question arising from this
- what's the difference between
template<class Vand
template<typename V?
I finally simply put all the code in one template member function (no
more specialized template)(code at end of post). This compiles fine,
except for the following calls (other calls are OK):
LatticeParameters::writeValueXml(latticeElem, "FileDate", bufferDate);
LatticeParameters::writeValueXml(latticeElem, "RandomSeed",
random->getRandomSeed());
where bufferDate is a /char bufferDate[100]/ I get the error
undefined reference to `void LatticeParameters::writeValueXml<char
[100]>(TiXmlElement*, std::basic_string<char, std::char_traits<char>,
std::allocator<char const&, char const (&) [100],
std::basic_string<char, std::char_traits<char>, std::allocator<char
const&, std::basic_string<char, std::char_traits<char>,
std::allocator<char const&, int, int)'
(I can make this error go away by transforming my char[100] into a string)
and random->getRandomSeed() returns /unsigned long getRandomSeed();/ I
get the error
undefined reference to `void LatticeParameters::writeValueXml<unsigned
long>(TiXmlElement*, std::basic_string<char, std::char_traits<char>,
std::allocator<char const&, unsigned long const&,
std::basic_string<char, std::char_traits<char>, std::allocator<char
const&, std::basic_string<char, std::char_traits<char>,
std::allocator<char const&, int, int)'
Could you explain this behavior to me? Thank you very much
Best regards
Phil
-- prototype --
template<class Vstatic void writeValueXml(TiXmlElement* atomElem,
const string& xmlElemName, const V& value,
const string& attributeName = "",
const string& attributeValue = "",
int leadingBlankSpaces = 28,
int trailingBlankSpaces = 10);
-- def (simplified) --
template<class V>
void LatticeParameters::writeValueXml(TiXmlElement* atomElem,
const string& xmlElemName, const V& value,
const string& attributeName,
const string& attributeValue,
int leadingBlankSpaces,
int trailingBlankSpaces){
std::stringstream converter;
converter << value;
string v = converter.str();
cout << v << endl;
}