Is template deny the principles of strong type?

according to wiki

Weak typing allows a value of one type to be treated as another, for example treating a string as a number.[29] This can occasionally be useful, but it can also allow some kinds of program faults to go undetected at compile time and even at run-time.

Strong typing prevents the above. An attempt to perform an operation on the wrong type of value raises an error.[29] Strongly typed languages are often termed type-safe or safe.


I am not quite understand the meaning of strong type
template would give you some warnings if you generate a wrong type
Is template deny the principles of strong type or it enhance the ability of strong type?
Thanks a lot
I am not quite understand the meaning of strong type
It's pretty much as it says. You can't do this:
 
std::string s = 7;  // there's no defined conversion and the strong typing stops this 


template would give you some warnings if you generate a wrong type
If you attempt a type mismatch like this with a template type, the result is the same; a syntax error.

s template deny the principles of strong type or it enhance the ability of strong type?
No. Do you understand what a timeplate is?
No. Do you understand what a timeplate is?
at first I thought I know, but now I am quite confuse
I thought template is somekind of candy for programmer
it would generate the code you ask for
and it is same as handcrafted codes

but my senior say template would violate the rule of strong type
yet he didn't give me an answer I could accept
what make template violate the principles of strong type?
I thought template is somekind of candy for programmer it would generate the code you ask for and it is same as handcrafted codes
I'm not sure what you mean by that.

Consider a function that returns the larger of two ints
1
2
3
4
int Max(int a, int b)
{
    return (a < b) ? b : a;
}
and another for strings:
1
2
3
4
std::string Max(std::string a, std::string b)
{
    return (a < b) ? b : a;
}


As you can see, the algorithm doesn't vary, it's just the type that does. And as long as < does the right thing it should work as it seems. A template version will generate an instance of that function for every type with which it's used. So you get different functions for each different type. But each instance generated is typesafe.

There's no "candy" here. Every feature is an engineering tradeoff, and templates are no different in that respect.
Last edited on
There's no "candy" here. Every feature is an engineering tradeoff, and templates are no different in that respect.

the meaning of "candy" is I don't need to copy and paste but only need to change the argument of template
And the result is same as handcrafted code, this could save us a lot of times
so I think it is a "candy" for programmers
My senior insist that template is not type safe yet he can't give me any example or reosan
Last edited on
My senior insist that template is not type safe
Templates are typesafe.
My senior insist that template is not type safe yet he can't give me any example or reosan
Counterexample:
1
2
3
4
5
6
7
8
9
10
template <typename T1,typename T2>
void assign(T1 &a,T2 &b){
    a=b;
}

//...
int a,b;
std::string c;
assign(a,b);
assign(a,c); //Error. 

Tell your senior that appeal to authority is a fallacy, and that he should shut up if he can't substantiate what he says.
Thanks a lot. Looks like I could belief that template is type safe

Tell your senior that appeal to authority is a fallacy, and that he should shut up if he can't substantiate what he says

Then next day there would be no room for me
FYI, yes, templates are programmer's candy: The compiler writes all your classes for you. Templates may not show obvious errors depending on type right on the spot, but most certainly they will arise in compilation time. For example:

1
2
3
4
5
template<class T>
class SomeClass
{
    static void DoSomeStuff() { T::SomeStaticMethod(); }
};


The code above assumes that all types used in the template have the static method 'SomeStaticMethod' defined. That is an error that you might miss if you call for the template using a data type that doesn't expose such a method. You don't easily get this error shown when you write the code, but the compiler will in fact catch this.
My senior insist that template is not type safe yet he can't give me any example or reosan

It's understandable that you would get that impression from reading what a template is. But the fact that they actually think this way means they have avoided using Templates themselves. Type casting for a template is done at the initialization of the object or calling the function when the code gets compiled, not at run time. So this would be enough evidence to support the idea of Templates being type safe.

On a side note, it's hard for me to believe that anyone with a credible amount of experiance has never used any of the STL containers. Does your senior realise that's what they are?
So this would be enough evidence to support the idea of Templates being type safe.

Thanks a lot. I think your explanations are easier to understand and more logical than my senior.
I show him the words of "c++ templates the complete guide"
But he says : "Do you really believe that shit could be eaten?Don't you have any abilities to
discern what is right and wrong but believe everything in books?"
He is so confident, and this make me confuse about who is right

Does your senior realise that's what they are?

He totally refuse to use stl and believe that handcrafted code is always the best
And he also believe that those who like to use stl or some open source lib like boost
is nothing but some losers who don't have the abilities to develop the lib by themselves
He always says : "The real master piece would never be free, every free libs are not
good enough to compare with handcrafted code, if you can't write a better code
than those free libs, that means you are too green"

I don't have the abilities to develop my own stl yet, even in the future I don't
think I would have the abilities and times to develop all of the good things done by those
famous libs(sgi stl, qt4, boost, wxWidget, loki and others).
Even I have the times and finally become a good programmers
it is very hard to believe that I can make it as good as
them, because every specific field need different specific knowledge.

Whatever, thanks for all of your explanations.I think my senior make a mistake, but I have
given up to persuade him.
He totally refuse to use stl and believe that handcrafted code is always the best
And he also believe that those who like to use stl or some open source lib like boost
is nothing but some losers who don't have the abilities to develop the lib by themselves


He's a retard. Don't believe a word that comes out of his mouth.
"The real master piece would never be free, every free libs are not
good enough to compare with handcrafted code, if you can't write a better code
than those free libs, that means you are too green"


ROFL. Tell him to go rewrite boost and sell it for cash then. What a ****ing idiot.
LOL. I feel sorry for you stereoMatching. If I were you I think I would put my resume in the mail for someone else so you can get away from your senior as quickly as possible -- he is going to ruin the company sooner or later.

Here is some fun:
http://en.wikipedia.org/wiki/Mel_Kaye

Also:
http://www.catb.org/jargon/html/R/Real-Programmer.html

LOL! :-]
Wow. That's a level of stupidity I never thought anyone could achieve. And you're actually nice enough to call him "senior". I'd call him "the village idiot". It's like he purposely set out to become the worst possible programmer he could be, which I guess is admirable (in a retarded kind of way).
He certainly seems to be doing quite well at it, if not having outright succeeded. That kind of view is so idiotic it's almost unbelievable.
You all act as if this is uncommon. (It isn't, alas.)
Topic archived. No new replies allowed.