Topic: Possible ambiguity in parsing complex depending on locale ?


Author: Sylvain Pion <Sylvain.Pion@sophia.inria.fr>
Date: Thu, 22 Mar 2007 09:46:35 CST
Raw View
Hi,

I am wondering if there can be an ambiguity in parsing std::complex
from a stream, depending on the locale.  The standard says:

template<class T, class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is , complex<T>& x );

Effects: Extracts a complex number x of the form: u, (u), or (u,v),
where u is the real part and v is the imaginary part.


Now, what happens if the decimal point is a comma, following the locale?
In that case, "(1,2)" could be parsed in 2 different ways, in practice
ending up with the complex number being the real number 1.2 instead
of (1,2).

I am not very familiar with how locales work, so I don't know if they
can affect this stream operator.


The context of this question is that I have the same issue for the
std::interval proposal (N2137), where intervals can be read as [u]
or [u,v].

Thanks for any answer and advice.

--
Sylvain

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Sylvain.Pion@sophia.inria.fr (Sylvain Pion)
Date: Thu, 22 Mar 2007 15:12:47 GMT
Raw View
Hi,

I am wondering if there can be an ambiguity in parsing std::complex
from a stream, depending on the locale.  The standard says:

template<class T, class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is , complex<T>& x );

Effects: Extracts a complex number x of the form: u, (u), or (u,v),
where u is the real part and v is the imaginary part.


Now, what happens if the decimal point is a comma, following the locale?
In that case, "(1,2)" could be parsed in 2 different ways, in practice
ending up with the complex number being the real number 1.2 instead
of (1,2).

I am not very familiar with how locales work, so I don't know if they
can affect this stream operator.


The context of this question is that I have the same issue for the
std::interval proposal (N2137), where intervals can be read as [u]
or [u,v].

Thanks for any answer and advice.

--
Sylvain

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: pjp@dinkumware.com ("P.J. Plauger")
Date: Thu, 22 Mar 2007 16:48:18 GMT
Raw View
"Sylvain Pion" <Sylvain.Pion@sophia.inria.fr> wrote in message
news:ettnao$680$1@news-sop.inria.fr...

> I am wondering if there can be an ambiguity in parsing std::complex
> from a stream, depending on the locale.  The standard says:
>
> template<class T, class charT, class traits>
> basic_istream<charT, traits>&
> operator>>(basic_istream<charT, traits>& is , complex<T>& x );
>
> Effects: Extracts a complex number x of the form: u, (u), or (u,v), where
> u is the real part and v is the imaginary part.
>
>
> Now, what happens if the decimal point is a comma, following the locale?
> In that case, "(1,2)" could be parsed in 2 different ways, in practice
> ending up with the complex number being the real number 1.2 instead
> of (1,2).
>
> I am not very familiar with how locales work, so I don't know if they
> can affect this stream operator.
>
>
> The context of this question is that I have the same issue for the
> std::interval proposal (N2137), where intervals can be read as [u]
> or [u,v].
>
> Thanks for any answer and advice.

The result might be surprising to some, but shouldn't be ambiguous.
Extractors are greedy in consuming an input field, so if a comma
makes sense as part of the "u" field, it will be taken as part of
that field. The library has no obligation to back up and try again,
or to try two different parses and pick the "better" one.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: howard.hinnant@gmail.com (Howard Hinnant)
Date: Thu, 22 Mar 2007 16:48:36 GMT
Raw View
In article <ettnao$680$1@news-sop.inria.fr>,
 Sylvain Pion <Sylvain.Pion@sophia.inria.fr> wrote:

> Hi,
>
> I am wondering if there can be an ambiguity in parsing std::complex
> from a stream, depending on the locale.  The standard says:
>
> template<class T, class charT, class traits>
> basic_istream<charT, traits>&
> operator>>(basic_istream<charT, traits>& is , complex<T>& x );
>
> Effects: Extracts a complex number x of the form: u, (u), or (u,v),
> where u is the real part and v is the imaginary part.
>
>
> Now, what happens if the decimal point is a comma, following the locale?
> In that case, "(1,2)" could be parsed in 2 different ways, in practice
> ending up with the complex number being the real number 1.2 instead
> of (1,2).
>
> I am not very familiar with how locales work, so I don't know if they
> can affect this stream operator.
>
>
> The context of this question is that I have the same issue for the
> std::interval proposal (N2137), where intervals can be read as [u]
> or [u,v].
>
> Thanks for any answer and advice.

You're in good company:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#629

We're wondering too. :-)

Note that there's no proposed resolution.  :-(

-Howard

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "James Kanze" <james.kanze@gmail.com>
Date: Fri, 23 Mar 2007 13:43:28 CST
Raw View
On Mar 22, 5:48 pm, howard.hinn...@gmail.com (Howard Hinnant) wrote:
> In article <ettnao$68...@news-sop.inria.fr>,
>  Sylvain Pion <Sylvain.P...@sophia.inria.fr> wrote:
> > I am wondering if there can be an ambiguity in parsing std::complex
> > from a stream, depending on the locale.  The standard says:

> > template<class T, class charT, class traits>
> > basic_istream<charT, traits>&
> > operator>>(basic_istream<charT, traits>& is , complex<T>& x );

> > Effects: Extracts a complex number x of the form: u, (u), or (u,v),
> > where u is the real part and v is the imaginary part.

> > Now, what happens if the decimal point is a comma, following the locale?
> > In that case, "(1,2)" could be parsed in 2 different ways, in practice
> > ending up with the complex number being the real number 1.2 instead
> > of (1,2).

> > I am not very familiar with how locales work, so I don't know if they
> > can affect this stream operator.

> > The context of this question is that I have the same issue for the
> > std::interval proposal (N2137), where intervals can be read as [u]
> > or [u,v].

> You're in good company:

> http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#629

> We're wondering too. :-)

As Plauger pointed out, there's no wondering about what the
current standard requires.  Of course, what it requires makes it
more or less impossible to read and write complex values in most
European locales, but who cares about us Europeans:-)?

> Note that there's no proposed resolution.  :-(

There are, in fact, several; they just appear in the discussion
session.  (I personally favor the first, as it seems the
simplest and least likely to cause problems elsewhere.)

Note too that the issue is only dated from Jan. of this year, so
the committee hasn't yet had the time to discuss it.

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]