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 

Re: Typeof workaround

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





PostPosted: Sat Jun 28, 2003 11:32 pm    Post subject: Re: Typeof workaround Reply with quote



"Nicolas Fleury" <nid_oizo (AT) yahoo (DOT) com_removethe_> wrote

Quote:
Hi,

I would like to do something like:

template <typename X, typename Y
MyClass
However, typeof is not standard yet and I'm trying to find a workaround.
I have as a solution partial specializations for each combinaison, but
it's not very scalable, since I would like to support user-defined types
without burden for user. Is there another workaround?

Sorry, not really. If there were a scalable workaround, the standards
committee wouldn't even consider adding typeof to the language. There are
some alternatives to typeof but all of them require a change to the language
in one area or another. For now, you will be stuck in writing a lot of
redundant and repetitive code to worka round that omission.

sm



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

Back to top
Siemel Naran
Guest





PostPosted: Sun Jun 29, 2003 10:20 pm    Post subject: Re: Typeof workaround Reply with quote



"Nicolas Fleury" <nid_oizo (AT) yahoo (DOT) com_removethe_> wrote


Quote:
template <typename X, typename Y
MyClass MyClass<Y>&);



Start with template_if

template <bool cond, class A, class B>
struct template_if
{
};

template <class A, class B>
struct template_if<true,A,B>
{
typedef A result;
};

template <class A, class B>
struct template_if<false,A,B>
{
typedef B result;
};


Now create your plustype<X,Y> that captures the type of X+Y in a
typedef.

template <class X, class Y>
struct plustype{
typedef X first;
typedef Y second;
typedef typename template_if<sizeof(X()+Y())==sizeof(X), X,
Y>::result
result;
};

Assumptions:

(1) In X+Y, type of result is either X or Y. For the exceptions to this
rule users can specialize plustype.

(2) X and Y have different sizeof. Unfortunately this means plustype
will
fail for X == signed int and Y == unsigned int, where the result should
be Y
or unsigned int because unsigned takes precedence, but will be X.
Perhaps
std::numeric_limits<T>::is_signed would help for cases like these. So
how
should we define struct plustype now?

(3) X and Y have default constructors. For types that we are add, like
int
and double, the assumption makes sense.


Then write your operator+.

template <typename X, typename Y>
MyClass<typename plustype
operator+(const MyClass<X>&, const MyClass<Y>&);


--
+++++++++++
Siemel Naran


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

Back to top
Vincent Lascaux
Guest





PostPosted: Mon Jun 30, 2003 3:59 pm    Post subject: Re: Typeof workaround Reply with quote



If you use only types with different sizes (this is a big assertion, I
know...), you could use this code :

template<int ID> struct SizeToType { };

template<> struct SizeToType< sizeof(int) > { typedef int value; };
template<> struct SizeToType< sizeof(char) > { typedef char value; };
template<> struct SizeToType< sizeof(double) > { typedef double value;
};
// .....

template<class X, class Y>
struct ReturnType { typedef SizeToType<sizeof(X()+Y())>::value value; };


It uses sizeof(type) to get an "identifier" of the type.
If typeid gave an integer unique identifier, couldn't typeof( expression
)
be implemented as IDToType< typeid(expression).id() >::value (with an
overloading of the IDToType template structure as with the SizeToType
previously) ? I think that could be an easy way to implement it no ?

--
Vincent



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





PostPosted: Mon Jun 30, 2003 4:38 pm    Post subject: Re: Typeof workaround Reply with quote

With this good idea, we can get very close to the definition of the
typeof
keyword :

template<int i> struct MyMap { char value[i]; };
template<int ID> struct IDToType { };

#define DEFINE_TYPE(Type, ID)
template<> struct IDToType< sizeof(MyMap { typedef Type value;
};

static MyMap<ID> TypeToID(Type);

DEFINE_TYPE(int, 1)
DEFINE_TYPE(char, 2)
DEFINE_TYPE(double, 3)
// ....

#define Typeof(Expression) IDToType<sizeof(TypeToID(Expression))>::value

You can then use Typeof like that :
Typeof(int() + char()) is int
Typeof(double() + int()) is double
....

It will also work with your types, provided you give them an ID with the
DEFINE_TYPE macro. The only difficulty will be if you want to use
template
classes in a Typeof (because you'll have to give an ID to each type you
want
to use)

--
Vincent




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





PostPosted: Tue Jul 01, 2003 8:05 am    Post subject: Re: Typeof workaround Reply with quote

For this particular application, you can use some form of expression
templates. See http://osl.iu.edu/~tveldhui/papers/Expression-Templates/exprtmpl.html
for details.

The idea is to parse the expression at compile time and build a tree
(not necessarily a class hierarchy) of functor classes. Then you can
overload the suitable operator functions to build the tree
transparently for the client code.

--Emil

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





PostPosted: Wed Jul 02, 2003 5:00 pm    Post subject: Re: Typeof workaround Reply with quote

In message <3f016200$0$23960$626a54ce (AT) news (DOT) free.fr>, Vincent Lascaux
<nospam (AT) nospam (DOT) org> writes
Quote:
The problem is that typeid (expression).id () may not be a
compile-time constant. Ideally you want a system that mapped distinct
types to distinct integers or other compile-time constants.

What would be the difficulty to make typeid(expression).id() a conpile
time
constant ? The compiler just has to maintain an integer it increments
(at
compile time) whenever it finds a new class.


I am not sure how that would work with separate TUs.


--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU


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

Back to top
Sebastian Moleski
Guest





PostPosted: Wed Jul 02, 2003 10:12 pm    Post subject: Re: Typeof workaround Reply with quote

"Vincent Lascaux" <nospam (AT) nospam (DOT) org> wrote

Quote:
The problem is that typeid (expression).id () may not be a
compile-time constant. Ideally you want a system that mapped distinct
types to distinct integers or other compile-time constants.

What would be the difficulty to make typeid(expression).id() a conpile
time
constant ? The compiler just has to maintain an integer it increments (at
compile time) whenever it finds a new class.

Because typeid returns the dynamic type of an object. Particularly, in the
following case:

#include <memory>
#include <iostream>

class Base {
};

class Derived : public Base {
};

int main() {
std::auto_ptr<Base> p(new Derived);
std::cout << std::boolalpha << typeid(Base) == typeid(p.get()) <<
std::endl;
}

the program should print out "false" because typeid(p.get()) will return the
dynamic type (Derived) of what get() returns, not the static type (Base).

sm



[ 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.