Friday, May 15, 2015

Digest for comp.lang.c++@googlegroups.com - 25 updates in 5 topics

ram@zedat.fu-berlin.de (Stefan Ram): May 15 04:08PM

>and acts as an SCGI server. When an SCGI request comes from the HTTP
 
If I'd have to do this, I'd ask for the grammar of the SCGI
request language.
 
If there is no such grammar, then it is not specified what I
am actually supposed to implement.
 
If there is such a grammar, then I can write a parser.
 
Regular expressions are not always suitable to parse
expressions. Are you sure that there is always at most
one pattern that matches?
 
Think about implementing it in the most natural way for now:
 
if the request matches regexp0 then ...
else if the request matches regexp1 then ...
...
else if the request matches regexp9 then ...
 
(This approach might not be suitable when there are very
many such if-branches or the request syntax changes very
often at runtime.)
 
And hide it behind a concept (run-time interface).
Once the whole system is working, you can then still
"optimize" this part later /if/ need be.
 
»Some people, when confronted with a problem, think "I
know, I'll use regular expressions." Now they have two
problems.«
 
Jamie Zawinski in alt.religion.emacs
ram@zedat.fu-berlin.de (Stefan Ram): May 15 06:39PM

>In practice it is easy:
>Download boost and <boost/endian.hpp> figures it out compile time for fairly
>decent list of platforms.
 
Another means might be the Autoconf AC_C_BIGENDIAN feature.
GNU C has »endian.h«, some systems have »sys/param.h«.
ram@zedat.fu-berlin.de (Stefan Ram): May 15 07:35PM

> char ch = str.at(jj);
> if(ch < 32) str.erase(jj, 1);
>}
 
#include <iostream>
#include <ostream>
#include <string>
#include <algorithm>
 
using namespace ::std::literals;
 
int main()
{ ::std::string const source{ "alpha\0beta\0gamma"s };
::std::string target;
target.reserve( source.size() );
::std::remove_copy_if
( source.begin(), source.end(), ::std::back_inserter( target ),
[]( ::std::string::value_type const ch ){ return !ch; });
::std::cout << "target = " << target << '\n'; }
ram@zedat.fu-berlin.de (Stefan Ram): May 15 07:40PM

>bind(not_equal_to, 0)
 
or (with different, but maybe intended, semantics):
 
#include <cctype>
...
::std::not1( ::std::ptr_fun( ::std::iscntrl ))
 
.
ram@zedat.fu-berlin.de (Stefan Ram): May 15 07:43PM

>#include <cctype>
>...
>::std::not1( ::std::ptr_fun( ::std::iscntrl ))
 
What do you all think?
 
When both notations are possible, is it better to
use »point-free« notation (that is, without variables)
or »lambda« notation? E.g., comparing
 
bind( not_equal_to, 0 )
 
with
 
[]( int const point ){ return point != 0; }
 
.
ram@zedat.fu-berlin.de (Stefan Ram): May 15 10:12PM

>>decent list of platforms.
>Another means might be the Autoconf AC_C_BIGENDIAN feature.
>GNU C has »endian.h«, some systems have »sys/param.h«.
 
The following code might not always yield the intended
outcome as others have pointed out that some parts
are not portable or well-defined, but it /is/ compiled
with »constexpr«.
 
#include <iostream>
#include <ostream>
 
constexpr bool endian()
{ using bytes = struct { char a; };
using overlay = union { int i; bytes b; };
return overlay{ 1 }.b.a; }
 
int main()
{ ::std::cout << "endian()" << " = " << endian() << '\n'; }
 
One could add security / sanity checks, such as statically
asserting that sizeof( char)< sizeof( int ) or that one of
the bytes is one and all preceding bytes are zero.
Juha Nieminen <nospam@thanks.invalid>: May 15 06:44AM

Is it possible to determine endianess at compile time (in C++14)?
 
At runtime it can be determined, for example, like this:
 
bool isLittleEndianSystem()
{
unsigned value = 1;
unsigned char* ptr = (unsigned char*)&value;
return *ptr == 1;
}
 
Changing that to a constexpr function, however, doesn't seem so trivial
(unless I'm missing something obvious). Any ideas?
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
"Öö Tiib" <ootiib@hot.ee>: May 15 01:50AM -0700

On Friday, 15 May 2015 09:44:29 UTC+3, Juha Nieminen wrote:
> }
 
> Changing that to a constexpr function, however, doesn't seem so trivial
> (unless I'm missing something obvious). Any ideas?
 
Answer depends on if your question is of academical or of practical nature.
 
In theory with standard C++ it is impossible:
* There are no macros that help neither in C nor C++ ( and even not in POSIX).
* 'std::numeric_limits' specify nothing about order of bits.
* All the type punning through union or casts (that you need for manufacturing
such check) are clearly listed as things that do *not* produce constant
expressions by standard.
 
In practice it is easy:
Download boost and <boost/endian.hpp> figures it out compile time for fairly
decent list of platforms.
http://www.boost.org/doc/libs/1_58_0/boost/detail/endian.hpp
scott@slp53.sl.home (Scott Lurndal): May 15 02:32PM

>Is it possible to determine endianess at compile time (in C++14)?
 
on linux it is, regardless of the C++ version. Several of our
C++ projects need to run on both LE and BE systems, we have
this in a common header file then use the appropriate accessors
in the code. Yes, it works with C as well.
 
#include <endian.h>
#ifndef __BYTE_ORDER
#if defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
#define __BYTE_ORDER __BIG_ENDIAN
#elif !defined(__BIG_ENDIAN) && defined(__LITTLE_ENDIAN)
#define __BYTE_ORDER __LITTLE_ENDIAN
#define __BIG_ENDIAN 4321
#elif !defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
#define __BIG_ENDIAN 4321
#define __BYTE_ORDER __BIG_ENDIAN
#else
#error Unable to determine Endian mode

No comments: