Function for all integer types

P

Philipp

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);
}
 
V

Victor Bazarov

Philipp said:
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 V> void writeValueXml(TiXmlElement* atomElem,
string const& xmlElemName, V value) {
std::stringstream converter;
converter << value;
string v = converter.str();
writeValueXml(atomElem, xmlElemName, v);
}
...
};

If you have a non-template function 'writeValueXml' that takes
a ref to const string as its third argument, no problem should exist
when resolving...

V
 
P

Philipp

Victor said:
Why not write a template and let the compiler worry about resolving
the signed/unsigned issue?

class Parameters {
...
template<class V> void 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 V> and
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 V> static 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;
}
 
V

Victor Bazarov

Philipp said:
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 V> and
template<typename V> ?

Three typing strokes.
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)

You might want to do

Lattice...(latticeElem, "FileDate", string(bufferDate));
and random->getRandomSeed() returns /unsigned long getRandomSeed();/ I
get the error

undefined reference to `void
LatticeParameters::writeValueXml<unsigned long>(TiXmlElement*,




Could you explain this behavior to me? Thank you very much

Undefined reference is most likely from the fact that the linker never
gets its hands on the implementation of that function. Where is it?
Best regards
Phil

-- prototype --
template<class V> static void writeValueXml(TiXmlElement* atomElem,
const string& xmlElemName, const V& value,
const string& attributeName = "",
const string& attributeValue = "",
int leadingBlankSpaces = 28,
int trailingBlankSpaces = 10);

-- def (simplified) --

WHERE? Did you put it in a separate C++ translation unit? Read
the FAQ about link errors associated with temlates.
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;
}

V
 
P

Philipp

Victor said:
WHERE? Did you put it in a separate C++ translation unit? Read
the FAQ about link errors associated with temlates.

Thank you again for your help. It now works fine!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top