473,480 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

typeof and g++

Hi there,
g++'s typeof is a very useful feature!
Will it never be in the Standard?
Is there any compliant way to implement it?
If I mean a function template returning a type, maybe is this
impossible?
What's about g++'s typeof implementation; how does it
work internally?

Thank.

--
fabioppp
Jul 23 '05 #1
10 5244
> g++'s typeof is a very useful feature!

Could you please give an example where you use it?
I can't think of any scenario which can't be solved otherwise.

-- John
Jul 23 '05 #2
John Smith wrote:
g++'s typeof is a very useful feature!

Could you please give an example where you use it?
I can't think of any scenario which can't be solved otherwise.

-- John


A template function deduce a type, and then return some values. I have
to know the type of the returned value.
If I use some trick with sizeof I think I could avoid the use of typeof.
But in this situation is really difficult to use sizeof in some way.

--
Fabio
Jul 23 '05 #3
fabioppp wrote:
A template function deduce a type, and then return some values. I have
to know the type of the returned value.
If I use some trick with sizeof I think I could avoid the use of typeof.
But in this situation is really difficult to use sizeof in some way.

For run-time type identification (RTTI) standard C++ provides
dynamic_cast. For extended type information it provides typeid.
Chapter 15 of TC++PL covers these. A snip from there:

"The dynamic_cast operator serves most needs for information about the
type of an object at run-time. Importantly, it ensures that code written
using it works correctly with classes derived from those explicitly
mentioned by the programmer. Thus, dynamic_cast preserves flexibility
and extensibility in a manner similar to virtual functions.

However, it is occasionally essential to know the exact type of an
object. For example, we might like to know the name of the object’s
class or its layout. The typeid operator serves this purpose by yielding
an object representing the type of its operand."

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4
fabioppp wrote:
Hi there,
g++'s typeof is a very useful feature!
Will it never be in the Standard?
It will, eventually. There is a proposal under discussion:
http://www.osl.iu.edu/~jajarvi/publi...type_n1478.pdf
Is there any compliant way to implement it?
If I mean a function template returning a type, maybe is this
impossible?


It is possible. See BOOST_TYPEOF proposal (typeof.zip) in the boost
sandbox file vault:

http://boost-sandbox.sourceforge.net/vault/

Regards,
Arkadiy

Jul 23 '05 #5
fabioppp wrote:
A template function deduce a type, and then return some values. I have
to know the type of the returned value.
If I use some trick with sizeof I think I could avoid the use of typeof.
But in this situation is really difficult to use sizeof in some way.


Please give a more detailed example. gcc's/g++'s typeof is entirely
compile-time, so surely standard C++ templates are able to provide your
desired functionality.

The only useful uses for typeof I know of are macros. It gives them
template-like powers, so that you're able to get something close to
generic programming in C. In C++, you shouldn't need it.

~phil
Jul 23 '05 #6
I think the best example is when you need to alocate a named object of
some expression-template (using Boost.Lambda):

lambda_functor<
lambda_functor_base<
logical_action<and_action>,
tuple<
lambda_functor<
lambda_functor_base<
relational_action<greater_action>,
tuple<
lambda_functor<placeholder<1> >,
int const


, lambda_functor<
lambda_functor_base<
relational_action<less_action>,
tuple<
lambda_functor<placeholder<2> >,
int const


f = _1 > 15 && _2 < 20;

as opposed to just (using auto that's defined through typeof):

auto f = _1 > 15 && _2 < 20;

Of course, this is not necessary, you can use the one above. The
question, however, is which one do you prefer :-)

Regards,
Arkadiy

Jul 23 '05 #7
Arkadiy wrote:
I think the best example is when you need to alocate a named object of
some expression-template (using Boost.Lambda):

lambda_functor<
[<...<...<..<blah, <blah>, blah>...>...>...> ]
>


f = _1 > 15 && _2 < 20;

as opposed to just (using auto that's defined through typeof):

auto f = _1 > 15 && _2 < 20;


I'm not using expression template.
It's about a compile time reflection. I have not a tree of expressions,
but a tree of types. It's the same.
It's a problem of time... I could need hours to write the right
[<...<...<..<blah, <blah>, blah>...>...>...> ], while with typeof I
could shut down my mind... and the compiler does the rest...

--
fabioppp
Jul 23 '05 #8
fabioppp wrote:
I'm not using expression template.
It's about a compile time reflection. I have not a tree of expressions, but a tree of types. It's the same.


In my example the result type of an expression is a tree of types
(templates). In modern C++ people use template parameter deduction
mechanism all the time to simplify creation of objects of complicated
types. As long as all that is needed is to pass this expression
somewhere, typeof is not necessary:

for_each(v.begin(), v.end(), cout << _1 << "\n");

Once you need to allocate a named object, though, it turnes out that
the actual type of the innocent-looking cout << _1 << "\n" functor is
similar to what I showed above, and you are much better off using
typeof/auto.

auto fun = cout << _1 << "\n";

Regards,
Arkadiy

Jul 23 '05 #9
Arkadiy wrote:
fabioppp wrote:
Hi there,
g++'s typeof is a very useful feature!
Will it never be in the Standard?


It will, eventually. There is a proposal under discussion:
http://www.osl.iu.edu/~jajarvi/publi...type_n1478.pdf


This is a pretty old reference. A newer one is here:

http://www.open-std.org/jtc1/sc22/wg...2004/n1705.pdf

See also

http://www.open-std.org/jtc1/sc22/wg...2004/n1721.pdf

Jonathan
Jul 23 '05 #10

John Smith wrote:
Could you please give an example where you use it?
I can't think of any scenario which can't be solved otherwise.


I could give a real-life practical example (unrelated to g++) where a
typeof would be a very useful feature.

A couple of weeks back I was "porting" some Windows code using MFC from
the VC++6.0 compiler to the 7.1 compiler. The point here is that some of
the types used have changed in a rather significant way between the two
versions of MFC being used. In particular file positions and time values
have changed from 32-bit to 64-bit entities. Of course, as typical in
MFC, such stuff doesn't have its own type declaration, but rather tends
to rely on "size-specific" typedefs.

The code goes somewhat along the lines of:

DWORD pos = file.GetPosition(); // CFile file;
// ...some more reading from the file
file.Seek( pos, CFile::begin );
Except this doesn't work with large files as supported in the updated
MFC version... (Luckily the compiler warns!) Here the return type of
GetPosition and the corresponding argument type to Seek has changed to
ULONGLONG instead, and I don't want to change all such declarations,
just in order to find out that I have to change them again on the next
update!

In this case I think the most elegant solution would be to be able to
write simply

typeof(file.GetPosition()) pos = file.GetPosition();
// ...
file.Seek( pos, CFile::begin );
The second-best solution is to use my own typedef, with a conditional
compile to define that one. As you say, it _can_ be solved otherwise,
but the typeof alternative would be much more maintainable.

--
Regards,
-+-Ben Hetland-+-
Jul 23 '05 #11

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

Similar topics

4
299
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in...
4
536
by: ichor | last post by:
hi what is the use of the typeof keyword , and how does it differ from the GetType method. i found the GetType method useful but i fail to understand the use of the typeof method. i have tried...
7
9922
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
950
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
1
1615
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' '...
11
5594
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
2325
by: Andrew Robinson | last post by:
I am guessing there is a simple solution but given a type T, how can I check for nullability? how can I accomplish the following? bool nullable = typeof(int).IsNullable; // false bool...
4
7091
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the...
20
9238
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
20
21700
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof()...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.