Monday, February 24, 2014

comp.lang.c++ - 26 new messages in 7 topics - digest

comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en

comp.lang.c++@googlegroups.com

Today's topics:

* could an braced-init-list be a first-class expression? - 6 messages, 2
authors
http://groups.google.com/group/comp.lang.c++/t/4790566113c6b180?hl=en
* Flummoxed - Please Help! - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/d4b5a6ac1e14e414?hl=en
* Iterator pair as a function argument? - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/d7a4099890dfa4ed?hl=en
* Nested Boost::unordered_map with std::pair insertion help .. - 2 messages, 2
authors
http://groups.google.com/group/comp.lang.c++/t/0864176555b94b56?hl=en
* OT: Problem building libc++ - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/177ee9b847d7540f?hl=en
* Working with Large Values (double) - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/e42843c9cdf13724?hl=en
* implicit passing by reference - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/dddf6db3d26e03e4?hl=en

==============================================================================
TOPIC: could an braced-init-list be a first-class expression?
http://groups.google.com/group/comp.lang.c++/t/4790566113c6b180?hl=en
==============================================================================

== 1 of 6 ==
Date: Wed, Feb 19 2014 10:47 am
From: pietro.cerutti@gmail.com


I'm wondering what would prevent an initialization list from being a first-class expression of type std::initializer_list.

I understand that the current standar allows braced-init-lists to appear only when assigning to a scalar of type T or to an object of a class taking an std::initializer_list in the assignment operator.

Having

void f (std::vector<double> v);

It is currently possible to do this:

auto a {1, 2, 3}:
g (a);

but not to do this:

g ({1, 2, 3});

As I see it, all information the compiler needs is there. Is there any specific reason why this isn't possible?

Thanks,




== 2 of 6 ==
Date: Wed, Feb 19 2014 10:48 am
From: pietro.cerutti@gmail.com



> g (a);

> g ({1, 2, 3});

of course I meant f(..);




== 3 of 6 ==
Date: Wed, Feb 19 2014 11:48 am
From: Paavo Helde


pietro.cerutti@gmail.com wrote in
news:a45ccd07-3838-4fda-b221-3a9deb589e70@googlegroups.com:
>
> void f (std::vector<double> v);
>
> It is currently possible to do this:
>
> auto a {1, 2, 3}:
> g (a);
>
> but not to do this:
>
> g ({1, 2, 3});

FWIW, gcc 4.8 (with -std=c++0x) compiles and runs this fine:

#include <vector>

void g(std::vector<double> v) {}

int main(int argc, char *argv[]) {
g({1.5, 2.0, 3.0});
}





== 4 of 6 ==
Date: Wed, Feb 19 2014 11:55 pm
From: Pietro Cerutti


Correct. This is what I get from posting trying to recall what the problem was. The actual issue is this. Why can't I construct a temporary initializer_list object with curly braces, so I can call this?


void h (const double d[3]);

h ({1., 2., 3.}.begin());




== 5 of 6 ==
Date: Thurs, Feb 20 2014 12:54 am
From: Paavo Helde


Pietro Cerutti <pietro.cerutti@gmail.com> wrote in
news:d8738a16-7951-43b1-befb-f1337390a040@googlegroups.com:

> Correct. This is what I get from posting trying to recall what the
> problem was. The actual issue is this. Why can't I construct a
> temporary initializer_list object with curly braces, so I can call
> this?
>
>
> void h (const double d[3]);
>
> h ({1., 2., 3.}.begin());

... whereas this compiles and runs:

auto x {1.};
typedef decltype(x) T;
h (T({1., 2., 3.}).begin());

Right... Something to do with grammar and that '}' can't be followed by
'.'?






== 6 of 6 ==
Date: Thurs, Feb 20 2014 1:10 am
From: Pietro Cerutti


On Thursday, February 20, 2014 9:54:18 AM UTC+1, Paavo Helde wrote:
> Pietro Cerutti <pietro.cerutti@gmail.com> wrote in
>
> news:d8738a16-7951-43b1-befb-f1337390a040@googlegroups.com:
>
>
>
> > Correct. This is what I get from posting trying to recall what the
>
> > problem was. The actual issue is this. Why can't I construct a
>
> > temporary initializer_list object with curly braces, so I can call
>
> > this?
>
> >
>
> >
>
> > void h (const double d[3]);
>
> >
>
> > h ({1., 2., 3.}.begin());
>
>
>
> ... whereas this compiles and runs:
>
>
>
> auto x {1.};
>
> typedef decltype(x) T;
>
> h (T({1., 2., 3.}).begin());
>
>
>
> Right... Something to do with grammar and that '}' can't be followed by
>
> '.'?

Yep my point exactly. The type info is there.





==============================================================================
TOPIC: Flummoxed - Please Help!
http://groups.google.com/group/comp.lang.c++/t/d4b5a6ac1e14e414?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Feb 19 2014 10:50 am
From: mrc2323@cox.net (Mike Copeland)


In article <k358g9he7j8er870k98fkah53kjr42m31r@4ax.com>,
geoff@invalid.invalid says...
> > I have the following (rather simple, I think) code which compiles but
> >executes in a bizarre way: the code in the subprogram is skipped when
> >called. 8<{{
> > Here's the code and the call to it I use:
> >
>
> What is this function supposed to do?

It's the start of a routine that will "spell a number": to produce
the sort of string data that's printed on a check. For example, 123
yields "One Hundred Twenty Three". The code that processes the
conversion isn't yet written...
>
> >string spellNumber(double value)
> >{
> > bool showThousands = false;
> > bool allZeros = true;
> > int ii, dPos, nn;
> > char wc;
> > string digits, temp;
> > ostringstream ossw;
> > static string builder;
> > ossw.str("");
> > ossw << value;
> > digits = ossw.str();
> > dPos = digits.find('.');
> > if(dPos != string::npos) digits.erase(dPos);
>
> Line above erases the decimal point and all after. Your string is now
> "123". Why do you need to do anything else to this string?

See above. I will process the cents part separately. Ultimately I
want to print checks with the output from this routine.
>
> > nn = digits.length();
> > wc = digits.back();
>
> OK, nn is 3, wc is '3'. Now what?
>
> > for(ii = digits.length()-1; ii >= 0; ii--)
> > {
> > int ndigit = (int)(digits[ii]-'0');
> > int column = (digits.length()-(ii+1));
> > } // for
>
> WTF was this done for? You could have easily just said ndigit = nn. I
> don't know what you are going to do with the column variable but they
> both go out of scope when the for loop is completed, ii is 0xffffffff
> (-1?) which suggests something went wrong in the loop.
>
Again, I'm testing only part of the code logic. 8<}}

> >// more code to be added here
> > return builder;
> >}
>

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com





== 2 of 2 ==
Date: Wed, Feb 19 2014 1:03 pm
From: Geoff


On Wed, 19 Feb 2014 11:50:19 -0700, mrc2323@cox.net (Mike Copeland)
wrote:

>In article <k358g9he7j8er870k98fkah53kjr42m31r@4ax.com>,
>geoff@invalid.invalid says...
>> > I have the following (rather simple, I think) code which compiles but
>> >executes in a bizarre way: the code in the subprogram is skipped when
>> >called. 8<{{
>> > Here's the code and the call to it I use:
>> >
>>
>> What is this function supposed to do?
>
> It's the start of a routine that will "spell a number": to produce
>the sort of string data that's printed on a check. For example, 123
>yields "One Hundred Twenty Three". The code that processes the
>conversion isn't yet written...

You misunderstood the intent of my comments. They were intended to
tell you to properly document your code. State the purpose of the
function, THEN write the function.

>>
>> >string spellNumber(double value)
>> >{
>> > bool showThousands = false;
>> > bool allZeros = true;
>> > int ii, dPos, nn;
>> > char wc;
>> > string digits, temp;
>> > ostringstream ossw;
>> > static string builder;
>> > ossw.str("");
>> > ossw << value;
>> > digits = ossw.str();
>> > dPos = digits.find('.');
>> > if(dPos != string::npos) digits.erase(dPos);
>>
>> Line above erases the decimal point and all after. Your string is now
>> "123". Why do you need to do anything else to this string?
>
> See above. I will process the cents part separately. Ultimately I
>want to print checks with the output from this routine.
>>
>> > nn = digits.length();
>> > wc = digits.back();
>>
>> OK, nn is 3, wc is '3'. Now what?
>>
>> > for(ii = digits.length()-1; ii >= 0; ii--)
>> > {
>> > int ndigit = (int)(digits[ii]-'0');
>> > int column = (digits.length()-(ii+1));
>> > } // for
>>
>> WTF was this done for? You could have easily just said ndigit = nn. I
>> don't know what you are going to do with the column variable but they
>> both go out of scope when the for loop is completed, ii is 0xffffffff
>> (-1?) which suggests something went wrong in the loop.
>>
> Again, I'm testing only part of the code logic. 8<}}
>

... and doing a very bad job of it by inspection. Those variables in
the for() loop go out of scope and can't be referenced outside that
loop. The loop becomes a no-op and yields nothing of interest. You
never documented why you need ndigit or column. You can get ndigit by
writing ndigit = digits.length() and immediately writing "one-hundred"
or "two-hundred" based on ndigit == 3 alone, the for loop becomes
superfluous.

You never addressed the issues of the IDE mentioned in my other posts
in this thread. What you are seeing is to be expected given the code
you've fed it. Your variables are going out of scope as you step
through the function and you are looking for them in all the wrong
places. Change your style, use white space, learn scoping rules and
declare your variables accordingly.

string spellNumber(double value)
{
int ndigit, column;
string digits;
ostringstream ossw;
static string builder;

// Convert integer portion of value to string
ossw.str("");
ossw << value;
digits = ossw.str();

// truncate the string
int dPos = digits.find('.');
if(dPos != string::npos)
digits.erase(dPos);

// Traverse characters in reverse order
int nn = digits.length();
char wc = digits.back();
for(int ii = digits.length()-1; ii >= 0; ii--)
{
ndigit = (int)(digits[ii]-'0');
column = (digits.length()-(ii+1));
}

return digits;
}





==============================================================================
TOPIC: Iterator pair as a function argument?
http://groups.google.com/group/comp.lang.c++/t/d7a4099890dfa4ed?hl=en
==============================================================================

== 1 of 4 ==
Date: Wed, Feb 19 2014 12:22 pm
From: nvangogh


Suppose I have a vector<int> values. Now the pair values.begin() and
values.end() will cover the range of the vector.

I am writing a simple function that takes the pair of iterators as
arguments along with an int value. The function then returns a bool to
indicate if the int value is found in the vector.

The first problem I have is to question if it is possible to pass
iterators as distinct arguments to a function? I would have thought that
the only way to accomplish this would be to pass the vector as a
reference, from there the function can use the iterators to do it's work.

I was thinking the correct definition would be

bool find_value (vector<int>&, int);

Or is there a way to pass an iterator pair as function arguments which I
have missed?




== 2 of 4 ==
Date: Wed, Feb 19 2014 12:23 pm
From: Ian Collins


nvangogh wrote:
> Suppose I have a vector<int> values. Now the pair values.begin() and
> values.end() will cover the range of the vector.
>
> I am writing a simple function that takes the pair of iterators as
> arguments along with an int value. The function then returns a bool to
> indicate if the int value is found in the vector.
>
> The first problem I have is to question if it is possible to pass
> iterators as distinct arguments to a function? I would have thought that
> the only way to accomplish this would be to pass the vector as a
> reference, from there the function can use the iterators to do it's work.
>
> I was thinking the correct definition would be
>
> bool find_value (vector<int>&, int);
>
> Or is there a way to pass an iterator pair as function arguments which I
> have missed?

std::find?

--
Ian Collins




== 3 of 4 ==
Date: Wed, Feb 19 2014 12:28 pm
From: Paavo Helde


nvangogh <nvangogh@pcexpert.net> wrote in news:cn8Nu.5082$1B3.554@fx26.am4:

> Suppose I have a vector<int> values. Now the pair values.begin() and
> values.end() will cover the range of the vector.
>
> I am writing a simple function that takes the pair of iterators as
> arguments along with an int value. The function then returns a bool to
> indicate if the int value is found in the vector.
>
> The first problem I have is to question if it is possible to pass
> iterators as distinct arguments to a function? I would have thought that
> the only way to accomplish this would be to pass the vector as a
> reference, from there the function can use the iterators to do it's work.
>
> I was thinking the correct definition would be
>
> bool find_value (vector<int>&, int);
>
> Or is there a way to pass an iterator pair as function arguments which I
> have missed?

There are plenty of STL functions like std::sort() which take iterators as
arguments, maybe you can take a look of their interfaces? BTW, your
function appears to be already implemented as std::find(), with iterator
arguments.

Cheers
Paavo





== 4 of 4 ==
Date: Wed, Feb 19 2014 1:03 pm
From: Jorgen Grahn


On Wed, 2014-02-19, Paavo Helde wrote:
> nvangogh <nvangogh@pcexpert.net> wrote in news:cn8Nu.5082$1B3.554@fx26.am4:
...
>> bool find_value (vector<int>&, int);
...
> There are plenty of STL functions like std::sort() which take iterators as
> arguments, maybe you can take a look of their interfaces? BTW, your
> function appears to be already implemented as std::find(), with iterator
> arguments.

Except not returning a bool. I might write a small wrapper
(untested):

template<class It, class Val>
bool contains(It a, It b, const Val& val) {
return std::find(a, b, val)!=b;
}

Sometimes the code becomes much more readable as
if(contains(a, b, 42) && !contains(b, c, 96)) ...
instead of
if(std::find(a, b, 42)!=b && std::find(b, c, 96)==c) ...

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .





==============================================================================
TOPIC: Nested Boost::unordered_map with std::pair insertion help ..
http://groups.google.com/group/comp.lang.c++/t/0864176555b94b56?hl=en
==============================================================================

== 1 of 2 ==
Date: Thurs, Feb 20 2014 4:32 am
From: Rahul Mathur


All,

I have having Boost boost::unordered_map nested map calling boost::unordered_map which calls std::map finally.

The LHS (towards RHS) side key for first boost::unordered_map is 'int' type followed by next key being char buffer value of size 10, and key for std::map being again a char buffer value of size 10. So the boost::unordered_map has nested map internally.
I have to insert the char buffer of size 10 (value being either 'ORANGE' or ORD123 as test case) into nested std::pair as defined below. After inserting, I have to search or find the value from char buffer of size 10.

The code as compiled on Linux is -

----
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <boost/unordered_map.hpp>
#include <map>
#include <algorithm>
#include <string>

using namespace std;

#if 0
#pragma pack(push, 2)
struct BookKeeping {
int ABCD;
char Apple_ID [10];
char Apple_NAME[10];
int Price;
int Number;
};
#pragma pack(pop)

No comments: