How would I stop implicit type conversion

  • Thread starter srinivasarao_moturu
  • Start date
S

srinivasarao_moturu

template<class T> Class ABC
{
public :
void SetValue(T k)
{ val = k; }

private:
T val;
};

int main()
{
ABC<int> tmp;
tmp.SetValue('A');
return 0;
}

In the above program compiler creates ABC<int> class because of
variable declaration tmp.

I passed character type to SetValue function so that there will be
implicit conversion from char to int and program works fine.

My doubt is, is there a way to capture this implicit conversion of
integral types by the compiler, instead of, smoothly converting from
char to int. My aim is, SetValue function should not accept the type
other Than T.
 
K

kwikius

My aim is, SetValue function should not accept the type
other Than T.

#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>

template<class T> class ABC
{
public :
template <typename T1>
typename boost::enable_if<
boost::is_same said:
SetValue(T1 k)
{ val = k; }

private:
T val;
};

regards
Andy Little
 
F

Frederick Gotham

template<class T> Class ABC


C++ is case-sensitive. The word you seek is "class", rather than "Class".

{
public :
void SetValue(T k)
{ val = k; }

private:
T val;
};

int main()
{
ABC<int> tmp;
tmp.SetValue('A');
return 0;
}

In the above program compiler creates ABC<int> class because of
variable declaration tmp.

I passed character type to SetValue function so that there will be
implicit conversion from char to int and program works fine.

My doubt is,


A more suitable word would be "question", rather than "doubt".

is there a way to capture this implicit conversion of
integral types by the compiler, instead of, smoothly converting from
char to int. My aim is, SetValue function should not accept the type
other Than T.


I don't know of any method off-hand. A possible solution would be to pass a
pointer.
 
E

Earl Purple

kwikius said:
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>

template<class T> class ABC
{
public :
template <typename T1>
typename boost::enable_if<

SetValue(T1 k)
{ val = k; }

private:
T val;
};

regards
Andy Little

I wish posters would give "how tos" in C++ without simply quoting
boost. That means one has to go and look up how boost did it, if you
can comprehend their code, that is.

Anyway, it looks like you start off with a template specialisation. My
guess is that you can start with something like this:

template< typename T1, typename T2 >
struct is_same
{
enum value = 0;
};

// template specialisation when T1 = T2

template < typename T >
struct is_same< T, T >
{
enum value = 1;
};

and then enable_if uses the value enum for different specialisations.

(Who was it that said that boost is template experimentation?)
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Earl said:
I wish posters would give "how tos" in C++ without simply quoting
boost.

Or better yet, that they write the complete solution and sign it with the
name of the OP.
 
K

kwikius

Earl said:
I wish posters would give "how tos" in C++ without simply quoting
boost. That means one has to go and look up how boost did it, if you
can comprehend their code, that is.

Google is but a click away my friend.

<cut code>

(Its a nice hot day here today and I'm gonna take the dog out in a bit
)...
wheeeeeeeeeeeeeeeeeh...
wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeh...
biddley bum...biddly boo...
wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeh...
biddley bee biddley boing...

Ahhhhhhhh thats better! :)
and then enable_if uses the value enum for different specialisations.

Nope. (hmm.. maybe this guy don't know how to use google?):

http://www.boost.org/libs/utility/enable_if.html
(Who was it that said that boost is template experimentation?)

Gotta problem with boost bud?

regards
Andy Little
 
D

Default User

red said:
Frederick said:
[redacted]

My doubt is,


A more suitable word would be "question", rather than "doubt".
I suspect this is a regional dialect issue. I've seen this many
times on the group.

It's been discussed extensively in the past. It seems to be the norm
for English speakers in India.




Brian
 
F

Frederick Gotham

Default User posted:
red said:
Frederick said:
[redacted]

My doubt is,


A more suitable word would be "question", rather than "doubt".
I suspect this is a regional dialect issue. I've seen this many
times on the group.

It's been discussed extensively in the past. It seems to be the norm
for English speakers in India.


(No intention to offend anyone here)

I thought it was only learners of English who confused the words
"question" and "doubt", and that is why I offered correction.

I wouldn't pretend to have more correct English than a native speaker of a
different dialect.
 
K

kwikius

Frederick said:
Default User posted:
red said:
Frederick Gotham wrote:
[redacted]

My doubt is,


A more suitable word would be "question", rather than "doubt".

I suspect this is a regional dialect issue. I've seen this many
times on the group.

It's been discussed extensively in the past. It seems to be the norm
for English speakers in India.


(No intention to offend anyone here)

I thought it was only learners of English who confused the words
"question" and "doubt", and that is why I offered correction.

I wouldn't pretend to have more correct English than a native speaker of a
different dialect.

Hi Troll ! .......... ;-)

just about as profound as this thread ...

http://tinyurl.com/rqcpb

Or should I call you Batman? .....Aka Troll!

regards
Andy Little
 
E

Earl Purple

kwikius said:
Gotta problem with boost bud?

Not really, although sometimes I feel that they make programmers like
me redundant.

It's just that when someone asks a language question as to how one can
do something in C++, I would like to see a C++ solution, not a quote of
a 3rd party library. A link will do so if you want to say that boost
has done it put in a link.
 
K

kwikius

Earl said:
Not really, although sometimes I feel that they make programmers like
me redundant.

I don't really see why that should be. IMO boost is quite a powerful
force in making sure that C++ continues to develop, thereby keeping you
in work and bear in mind that they do have a great influence on what
happens in the next version of C++. Furthermore the tools and libraries
at boost are free for commercial and non-commercial use, well
documented, well tested and useful. I cant remember if enable_if is
going to be in the next version of the C++ standard, but I am fairly
sure that is_same will be.
It's just that when someone asks a language question as to how one can
do something in C++, I would like to see a C++ solution, not a quote of
a 3rd party library. A link will do so if you want to say that boost
has done it put in a link.

I hope that most C++ programmers the world over know about boost by
now.

http://www.boost.org

If they don't then they only have to ask ... or Google.

regards
Andy Little
 
E

Earl Purple

kwikius said:
I don't really see why that should be.

Some of us happen to be reasonably good at writing utility libraries
but don't necessarily have a lot of business knowledge. I guess if I
learn all of boost I will have a big advantage in being able to apply
all this, although I doubt I'll be attending any interview anytime soon
where they test me on boost libraries. (I know, I might have said the
same about STL).

Where I work now, which is quite a large worldwide organisation (but is
not a software company), very little development is done in C++ now.
Java is by far the most popular language and C# has taken over as the
GUI language for Windows.

As for the comment about "experimentation with templates", the question
is how really useful some of these features are. Do we really need
enable_if? Is anyone in management going to make a design decision to
use C++ rather than a different language because it has enable_if ?
 
K

kwikius

Earl said:
Some of us happen to be reasonably good at writing utility libraries
but don't necessarily have a lot of business knowledge. I guess if I
learn all of boost I will have a big advantage in being able to apply
all this, although I doubt I'll be attending any interview anytime soon
where they test me on boost libraries. (I know, I might have said the
same about STL).

Where I work now, which is quite a large worldwide organisation (but is
not a software company), very little development is done in C++ now.
Java is by far the most popular language and C# has taken over as the
GUI language for Windows.

Sorry... I'm lost. Somehow boost is to blame for this?

regards
Andy Little
 
G

Geo

Where I work now, which is quite a large worldwide organisation (but is
not a software company), very little development is done in C++ now.
Java is by far the most popular language and C# has taken over as the
GUI language for Windows.

Maybe that paragraph says it all....

'not a software company'....'Java'...'C#'

I don't know with whom Java is 'popular', it looks and sounds horrible
to write in (though I'll admit I haven't had to) and every application
I've seen written in it has been horrible to use (unfortunately I've
had to). Really I can't see the point of these languages. They don't
seem to offer anything really worthwhile that can't be done in C++ (or
C for that matter). Compared to C++ Java looks like a toy and C# is
too proprietory.
 
E

Earl Purple

kwikius said:
Sorry... I'm lost. Somehow boost is to blame for this?

It isn't. And I guess I'm to blame if I'm not keeping up. By the way, a
surprising large numbers of developers do not know much about boost,
and if they do it's probably shared_ptr which is making its way into
tr1 anyway.

My first issue I guess was having to give up using my own smart pointer
which I had been using for about 2 years and was quite proud of. It
wasn't a particularly great smart pointer, it used intrusive reference
counting as did so many of the early ones. It had one or two other
issues too which took a big hack to get rid of but I thought I'd
improved it enough and was sad to let go.

I don't recall every really having my own string class. Not one to be
particularly fond of anyway.

I do still use my own tokeniser and string-convert functions. The
problem with the boost ones is the exceptions they throw are so useless
to me that I have to catch them and rethrow them anyway so I decided
not to bother with them.
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top