473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Function for all integer types

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);
}
Aug 7 '06 #1
4 1699
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);
}
...
};

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '06 #2
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;
}

Aug 7 '06 #3
Philipp wrote:
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?
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*,
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
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 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) --
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '06 #4
Victor Bazarov wrote:
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!
Aug 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
1
by: Ayokunle Giwa | last post by:
Why do I keep getting this error in the function to insert data below, any help would be apprecaited: CREATE OR REPLACE FUNCTION public.add_apppointment (date, time, integer,time, boolean, text,...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
10
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return...
8
by: hurry | last post by:
hi, I am writing a c program in VC++ 6. I have 2 files with 3 functions. file-1 having two functions "a" and "c" file-2 having a single function "b" with function "a" as main() , "a"...
12
by: eric.goforth | last post by:
Is there any reason to use: Private newPropertyValue As Integer Public ReadOnly Property MyProperty(ByRef MyParam as Integer) As Integer Get Return newPropertyValue End Get End Property
19
by: Robbie Hatley | last post by:
For your enjoyment, a function that expresses any integer with absolute value less-than-or-equal-to nine quintillion in any base from 2 to 36. (For larger bases, you could expand the "digit"...
9
by: wizwx | last post by:
what does the following mean? int func2(); According to C++, it surely means func2 is a function that takes no argument and returns an integer. But what about in C? Does it have the same...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.