C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What's wrong here? (templates)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Dhruv
Guest





PostPosted: Fri Oct 24, 2003 7:13 pm    Post subject: What's wrong here? (templates) Reply with quote



On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?



#include <iostream>

template <class T1, class T2>
struct same_type {
static const bool result = false;
};

template <class T1>
struct same_type<T1, T1> {
static const bool result = true;
};


template <bool Condition, class FT, class ST>
struct IF_ {
typedef ST type;
};

template <class FT, class ST>
struct IF_<true, FT, ST> {
typedef FT type;
};

template <class A, class B>
struct smaller_2 {
typedef typename IF_< (sizeof(A) < sizeof(B)), A, B>::type type;
};


template <class A, class B>
struct larger_size_2 {
typedef typename IF_< (sizeof(A) > sizeof(B)), A, B>::type type;
};

template <class A, class B, class C>
struct larger_size_3 {
typedef typename
IF_< (sizeof (typename larger_size_2 sizeof (C)),
typename larger_size_2<A, B>::type, C>::type type;
};

template <class A, class B, class C>
struct second_largest_3 {
typedef typename larger_size_3 <A, B, C>::type Exclude;
typedef typename
IF_ <same_type ::type first_type;
typedef typename
IF_ <same_type typename smaller_2<B, A>::type>::type second_type;
typedef typename larger_size_2 <first_type, second_type>::type type;
};

template <class A, class B, class C>
struct smallest_3 {
typedef typename
smaller_2 <typename second_largest_3 typename second_largest_3 <A, B, C>::first_type>::type type;
};


template <class A, class B, class C>
struct foo {
typename larger_size_3 <A, B, C>::type var1;
typename second_largest_3 <A, B, C>::type var2;
typename smallest_3 <A, B, C>::type var3;
// foo() { }
foo () {
var1 = "dhruv";
var2 = "dd";
var3 = "445";
}

};


int main ()
{
typedef double t1;
typedef double t2;
typedef double t3;
foo <t1, t2, t3> ofoo;
// if (sizeof(t1) > sizeof (t2))
// std::cout<<"True";
// else {
// std::cout<<"False";
// if (sizeof (t2) > sizeof (t3))
// std::cout<<"Yes";
// else
// std::cout<<"Nop ";
// }
}


Regards,
-Dhruv.











[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
sashan
Guest





PostPosted: Sat Oct 25, 2003 8:31 am    Post subject: Re: What's wrong here? (templates) Reply with quote



Dhruv wrote:

Quote:
On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?




It doesn;t compile because you can't assign a char* to a double. I changed

typedef double t1;
typedef double t2;
typedef double t3;


To

typedef char* t1;
typedef char* t2;
typedef char* t3;


and it compiles. It looks like you're trying to write code to check
given 3 types which one is the returns the largest sizeof value. I
haven't gone through it to see if it's correct but instead of writing a
template function larger_size_3<A,B,C> write a template function
larger<A, B> then larger_size_3<A,B,C> becomes equivalent to larger<
larger.

--
sashan
http://www.cs.auckland.ac.nz/~sgov008/





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Sun Oct 26, 2003 5:14 am    Post subject: Re: What's wrong here? (templates) Reply with quote



On Sat, 25 Oct 2003 04:31:39 -0400, sashan wrote:

Quote:
Dhruv wrote:

On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?




It doesn;t compile because you can't assign a char* to a double. I changed

typedef double t1;
typedef double t2;
typedef double t3;


To

typedef char* t1;
typedef char* t2;
typedef char* t3;




The code was meant to given an error to check which type is being
instantiated...


Quote:
and it compiles. It looks like you're trying to write code to check
given 3 types which one is the returns the largest sizeof value. I
haven't gone through it to see if it's correct but instead of writing a
template function larger_size_3<A,B,C> write a template function
larger<A, B> then larger_size_3<A,B,C> becomes equivalent to larger
larger<A,B>, C>.

I guess the code for checking the largest of the 3 is buggy, because all 3
are getting the same type(t2), which is in fact wrong...... Think you can
check it out for me?


Thanks,
-Dhruv.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Richard Howard
Guest





PostPosted: Wed Oct 29, 2003 1:28 am    Post subject: Re: What's wrong here? (templates) Reply with quote

Quote:
On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?

[snip]

Quote:
int main ()
{
typedef double t1;
typedef double t2;
typedef double t3;
foo <t1, t2, t3> ofoo;

but t1, t2, and t3 are all equivilant to double!?!

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Thu Oct 30, 2003 7:17 pm    Post subject: Re: What's wrong here? (templates) Reply with quote

On Tue, 28 Oct 2003 20:28:14 -0500, Richard Howard wrote:

Quote:
On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?

[snip]

int main ()
{
typedef double t1;
typedef double t2;
typedef double t3;
foo <t1, t2, t3> ofoo;

but t1, t2, and t3 are all equivilant to double!?!

Yes, but what if there are 3 different types (maybe user defined types)
that have the same size, and t1, t2, and t3 a defined as those types, and
you apply foo on those, then during instantiation, you will get var1,
var2, and var3 to be the same type, which is obviously not what you want.

Regards,
-Dhruv.







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
johnchx
Guest





PostPosted: Fri Oct 31, 2003 2:44 pm    Post subject: Re: What's wrong here? (templates) Reply with quote

"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote
Quote:
On Tue, 28 Oct 2003 20:28:14 -0500, Richard Howard wrote:

but t1, t2, and t3 are all equivilant to double!?!

Yes, but what if there are 3 different types (maybe user defined types)
that have the same size, and t1, t2, and t3 a defined as those types, and
you apply foo on those, then during instantiation, you will get var1,
var2, and var3 to be the same type,

No, you won't. (At least I don't when I try it.)

What seems to be happening is this: when gcc first instantiates a
template class, it "saves" the names that the programmer used for the
template parameters and uses them, if necessary, in warning and error
messages. Thus if you instantiate a template with a typedef,
subsequent error and warning messages will use that typedef name.
This usually leads to more comprehensible error messages.

Things get confusing, however, when you use the same instantiation
with different typedefs, as in

template < class T >
struct foo { typedef T type; };

typedef int t1;
typedef int t2;

foo< t1 >::type a; // Line A
foo< t2 >::type b; // Line B

At Line A, the compiler instantiates foo<int>, but saves the typedef
name "t1" for use in error messages. You'll get a warning about
"unused variable t1 a" on Line A.

At Line B, we create another instance of foo<int>, but the "user
friendly" name is already established -- it's "t1". So you'll get a
warning about "unused variable t1 b" on Line B.

Since the standard doesn't say anything about the contents of error &
warning messages, we can't call this a C++ bug. But you might want to
ask about it on the gcc list.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dickey Kentur
Guest





PostPosted: Sat Nov 01, 2003 10:00 am    Post subject: Re: What's wrong here? (templates) Reply with quote

"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote

Quote:
On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?



#include <iostream

template struct same_type {
static const bool result = false;
};

template struct same_type static const bool result = true;
};


If you pass a typedef and its' original type to same_type, you always
get result false here.
eg:
typedef int T;
same_type<int, T>::result // always yeilds false

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Sun Nov 02, 2003 1:31 am    Post subject: Re: What's wrong here? (templates) Reply with quote

On Sat, 01 Nov 2003 05:00:06 -0500, Dickey Kentur wrote:

Quote:
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote

On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?



#include <iostream

template struct same_type {
static const bool result = false;
};

template struct same_type static const bool result = true;
};


If you pass a typedef and its' original type to same_type, you always
get result false here.
eg:
typedef int T;
same_type<int, T>::result // always yeilds false

Ok, so I think I'm getting what you're saying. In the code, A and Exclude
will always give false when used with same_type, even if the type is the
same right?

Ok, so that's one flaw...

But, then, at least larger_size_3 should give t3 as the type, when in fact
it gives t2.

I think it's a combination of what you're saying, and what johnchx said,
and I guess I should report what he said as a bug. I've already reported 2
others today for gcc!!!

Regards,
-Dhruv.







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Sun Nov 02, 2003 1:33 am    Post subject: Re: What's wrong here? (templates) Reply with quote

On Fri, 31 Oct 2003 09:44:56 -0500, johnchx wrote:

Quote:
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote
On Tue, 28 Oct 2003 20:28:14 -0500, Richard Howard wrote:

but t1, t2, and t3 are all equivilant to double!?!

Yes, but what if there are 3 different types (maybe user defined types)
that have the same size, and t1, t2, and t3 a defined as those types, and
you apply foo on those, then during instantiation, you will get var1,
var2, and var3 to be the same type,

No, you won't. (At least I don't when I try it.)


What compiler, and what are the types of var1, var2, and var3?

[snip]


Quote:
Since the standard doesn't say anything about the contents of error &
warning messages, we can't call this a C++ bug. But you might want to
ask about it on the gcc list.

Yes, I'll do that...


Thanks,
-Dhruv.







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Tue Nov 04, 2003 1:12 am    Post subject: Re: What's wrong here? (templates) Reply with quote

On 1 Nov 2003 05:00:06 -0500, [email]jinz.di (AT) 163 (DOT) com[/email] (Dickey Kentur) wrote:

Quote:
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote

On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?

#include <iostream

template struct same_type {
static const bool result = false;
};

template struct same_type static const bool result = true;
};

If you pass a typedef and its' original type to same_type, you always
get result false here.
eg:
typedef int T;
same_type<int, T>::result // always yeilds false

Which broken compiler did you use to test that?

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Tue Nov 04, 2003 1:14 am    Post subject: Re: What's wrong here? (templates) Reply with quote

On 1 Nov 2003 20:31:54 -0500, "Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote:

Quote:
On Sat, 01 Nov 2003 05:00:06 -0500, Dickey Kentur wrote:

If you pass a typedef and its' original type to same_type, you always
get result false here.
eg:
typedef int T;
same_type<int, T>::result // always yeilds false

Ok, so I think I'm getting what you're saying. In the code, A and Exclude
will always give false when used with same_type, even if the type is the
same right?

Actually, the above will give true always. When the names are typedefs,
they are the same type. Typedef creates new names not new types.

Quote:
But, then, at least larger_size_3 should give t3 as the type, when in fact
it gives t2.

If it gave double, it would still be correct. A double by any other
name will smell as sweet.

Quote:
I think it's a combination of what you're saying, and what johnchx said,
and I guess I should report what he said as a bug. I've already reported 2
others today for gcc!!!

As johncx said, it is not a bug. Anyway, to get what you want when the
types are really different with the same size.

small2 B > A, A, B picks first when equal
large2 A > B, A, B picks second when equal
small3 small2<small2 picks leftmost when equal
large3 large2<large3 picks rightmost when equal
mid3
typedef typename
IF_<sizeof(B) < sizeof(A),
typename IF_ typename IF_ A>::type,
typename IF_<sizeof(C) < sizeof(B),
typename IF_ B>::type>::type type;
picks the one not picked by small3 or large3.

struct Ua { };
struct Ub { };
struct Uc { };
struct Ud { int d; };
struct Ue { int e; };

foo<Ua, Ub, Uc>,
foo<Ua, Ub, Ud>, foo<Ua, Ud, Uc>, foo<Ud, Ub, Uc>
foo<Ua, Ud, Ue>, foo<Ud, Ub, Ue>, foo<Ud, Ue, Uc>

All give three different results for small3, large3, mid3.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Tue Nov 04, 2003 5:14 pm    Post subject: Re: What's wrong here? (templates) Reply with quote

On Mon, 03 Nov 2003 20:14:59 -0500, John Potter wrote:

Quote:

Actually, the above will give true always. When the names are typedefs,
they are the same type. Typedef creates new names not new types.

Ok, so same_type would yield true...


Quote:
But, then, at least larger_size_3 should give t3 as the type, when in fact
it gives t2.

If it gave double, it would still be correct. A double by any other
name will smell as sweet.


When I've instantaited A = t2, B = t2, and C = t3, shouldn't the errors
reflect the typedefed types?


Quote:
As johncx said, it is not a bug. Anyway, to get what you want when the
types are really different with the same size.

small2 B > A, A, B picks first when equal

How, if sz(A) == sz(B), the B>A would be false, and B would get picked,
which is the 2nd template parameter, which is what I'm doing currently.


Quote:
large2 A > B, A, B picks second when equal
small3 small2<small2 picks leftmost when equal
Or rightmost?



If you are asking me to assume these thing about your code, then ignore
what I've just said.



Quote:
large3 large2<large3 picks rightmost when equal
mid3
typedef typename
IF_<sizeof(B) < sizeof(A),
typename IF_ typename IF_ A>::type,
typename IF_<sizeof(C) < sizeof(B),
typename IF_ B>::type>::type type;
picks the one not picked by small3 or large3.


Yes, this would work!!!


template <class A, class B, class C>
struct second_largest_3 {
typedef typename larger_size_3 <A, B, C>::type Exclude;
typedef typename
IF_ <same_type ::type first_type;

typedef typename
IF_ <same_type ::type,
C>::type second_type;

// typedef typename
// IF_ <same_type // typename smaller_2<A, B>::type>::type second_type;
typedef typename larger_size_2 <first_type, second_type>::type type;
};

Quote:
struct Ua { };
struct Ub { };
struct Uc { };
struct Ud { int d; };
struct Ue { int e; };

foo<Ua, Ub, Uc>,
foo<Ua, Ub, Ud>, foo<Ua, Ud, Uc>, foo<Ud, Ub, Uc
foo
All give three different results for small3, large3, mid3.


Ok, tried the code for all these inputs, and it works fine...


Thnaks,
-Dhruv.







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Tue Nov 04, 2003 7:52 pm    Post subject: Re: What's wrong here? (templates) Reply with quote

On 4 Nov 2003 12:14:49 -0500, "Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote:

Quote:
On Mon, 03 Nov 2003 20:14:59 -0500, John Potter wrote:

When I've instantaited A = t2, B = t2, and C = t3, shouldn't the errors
reflect the typedefed types?

Maybe. QoI. "Error - something wrong" is good enough for the standard.

Quote:
As johncx said, it is not a bug.

But the gcc team may consider it a bug in their error reporting.

Quote:
Anyway, to get what you want when the
types are really different with the same size.

small2 B > A, A, B picks first when equal

How, if sz(A) == sz(B), the B>A would be false, and B would get picked,
which is the 2nd template parameter, which is what I'm doing currently.

My original was

small2 A <= B, A, B

I screwed that up in the translation which should have been

small2 A > B, B, A picks first when equal

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.