473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C++0x auto and decltype

std::vector<int> v;
auto iter = v.begin();

Will this deduce iter to be std::vector<int>::iterator or const_iterator? I
have been searching for the answer in
http://www.open-std.org/jtc1/sc22/wg...2004/n1705.pdf but
couldn't find it. Does anybody know?

M.
Jul 23 '05 #1
4 1970
Marcin Kaliciñski wrote in
news:xE*******************@newsfe4-gui.ntli.net in comp.lang.c++:
std::vector<int> v;
auto iter = v.begin();

Will this deduce iter to be std::vector<int>::iterator or
const_iterator? I have been searching for the answer in
http://www.open-std.org/jtc1/sc22/wg...2004/n1705.pdf but
couldn't find it. Does anybody know?


The expression v.begin() above returns iterator, because v isn't
const, so the only type 'iter' can be is std::vector<int>::iterator.

The place to find the rules is in the current standard, as the auto
proposal builds on its template paramiter type deduction rules. So
you question boils down to:

template < typename T > void f( T ) {}
int main()
{
f( v.begin() );
}

I.e. What is the type of T above ?

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 23 '05 #2
Excuse the question .. but what exactly do I gain by using these
keywords? Apart from the fact that possibly I have to type less. Is
there anything I can do with these keywords, that I could not do without
them, or is this pure syntactic sugar like -> is?

thanks
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #3
Jakob Bieling wrote in news:d5*************@news.t-online.com in
comp.lang.c++:
Excuse the question .. but what exactly do I gain by using these
keywords? Apart from the fact that possibly I have to type less. Is
there anything I can do with these keywords, that I could not do
without them, or is this pure syntactic sugar like -> is?

thanks


Assume a set of overloaded functions:

some_type f( arg1_type a, arg2_type b );

Then:

template < typename A, typename B >
void contrived( A a, B b )
{
auto r = f( a, b );
std::cout << r << r;
}
Curently the /simple/ way to write the above means calling f(a, b)
twice, with possible side effects and expensive computations
accuring twice.

template < typename A, typename B >
void contrived( A a, B b )
{
std::cout << f( a, b ) << f( a, b );
}

Off the top of my head I've come up with two workaraounds:
1) Use indirection, i.e. put the body of contived in a
helper function, but that could be difficult with a less
simplistic example, or

2) Capture the return of f(a, b) in some kind of holder
object, but that would require an allocation and 2 virtual calls
(one for operator << and one for the destructor).

I'm sure there are others.

The basics of the proposal mean it will be simpler to write clear
and efficient code.

There is however a lot more to the auto/decltype proposal, but
it isn't that long so I suggest you give it a read.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 23 '05 #4
When you want to write a function that adds say matrix<T> and matrix<U>,
how would you define the type of result of your function? Naturally
you'll want it to be of type matrix<type_of_result_of_operation T+U>.
That's what auto and decltype are targeting.

Jakob Bieling wrote:
Excuse the question .. but what exactly do I gain by using these
keywords? Apart from the fact that possibly I have to type less. Is
there anything I can do with these keywords, that I could not do without
them, or is this pure syntactic sugar like -> is?

thanks


Jul 23 '05 #5

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

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
1
by: Luna Kwok | last post by:
Hello, I am having a problem with Netscape 7.01 & 7.02 not updating a variable value in javascript. I have stepped though my code with a debugger, and the values and code all look correct. This...
14
by: Ioannis Vranos | last post by:
I would like to see your views on these. C++98 is already a large language since it supports 4 paradigms and each one is supported well, with optimal space and time efficiency. And this is...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
2
jeffbroodwar
by: jeffbroodwar | last post by:
Hi, I want to concatenate 0x with a variable (dont know what type) to pass it in a byte array. the code below shows the original code : ORIGINAL CODE : ioData = new byte; ...
2
by: LewGun | last post by:
at the end of last year Herb Sutter told us that "C++ 0x has been fixed", now GCC4.3 had been released, the compiler were known as "the C ++ new features' experimental unit",but it support to the...
6
by: Dmitriy V'jukov | last post by:
On 2 Á×Ç, 20:47, "Dmitriy V'jukov" <dvyu...@gmail.comwrote: Q: Can I use Relacy Race Detector to check my algo againts other that C ++0x memory models (x86, PPC, Java, CLI)? A Yes, you...
13
by: Dmitriy V'jukov | last post by:
Consider following Peterson's algorithm implementation from Wikipedia: http://en.wikipedia.org/wiki/Peterson%27s_algorithm flag = 0 flag = 0 turn = 0 P0: flag = 1 turn = 1...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.