Friday, April 24, 2015

Digest for comp.lang.c++@googlegroups.com - 21 updates in 6 topics

Paavo Helde <myfirstname@osa.pri.ee>: Apr 23 11:35PM -0500

ram@zedat.fu-berlin.de (Stefan Ram) wrote in news:initialization-
 
> was »copy initialization«.
 
> Herb Sutter wrote »Copy initialization means the object is
> initialized using the copy constructor«.
 
So what? From a sentence like "Car washing is using water" you cannot
infer "Any usage of water means car washing".
 
 
> , the object »s« is initialized using the copy constructor.
> So then this is copy initialization, even if it uses
> parentheses?
 
This is terminology nitpicking; it boils down to whether you like to
think about the copy constructor in this case as a "single constructor
needed for constructing s from t" or "a copy initialization stage after
skipping the unneeded conversion of t into type of s". I guess logically
it could mean both. Fortunately, it seems latest C++ standards have given
up logical reasoning in this area and only talk about the *form* of the
statement (8.5/16, [dcl.init]):
 
"The initialization that occurs in the forms
T x(a);
T x{a};
as well as in new expressions (5.3.4), static_cast expressions (5.2.9),
functional notation type conversions (5.2.3), and base and member
initializers (12.6.2) is called direct-initialization."
 
So this is direct initialization, full stop.
 
Cheers
Paavo
"Norman J. Goldstein" <normvcr@telus.net>: Apr 24 03:23PM -0700

On 04/23/2015 03:36 PM, Stefan Ram wrote:
 
> , the object »s« is initialized using the copy constructor.
> So then this is copy initialization, even if it uses
> parentheses?
 
The two statements
 
::std::string s( "hello" );
 
and
 
::std::string s = "hello";
 
may, or may not, be compiled into the same code. It is up to the
compiler to decide whether to do the 2nd one as direct, or via a copy
constructor.
 
I'm not sure if the concept of "direct initialization" is well-defined,
but I would take it to mean that no constructors may be eliminated from
the generated code.
Christopher Pisz <nospam@notanaddress.com>: Apr 24 01:35PM -0500

The problem I am trying to solve is that I need to determine if any
given datetime falls within a certain scheduled week.
 
I am already using boost::posix_time::ptime in my project. I thought it
would be natural to use boost::posix_time::time_period and its
boost::posix_time::time_period::contains method.
 
However, it seems fruity... I wrote the following test:
 
// Boost Includes
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
 
// Standard Includes
#include <iostream>
#include <ostream>
#include <memory>
#include <vector>
 
int main()
{
boost::posix_time::ptime start(boost::gregorian::date(2015, 4, 24),
boost::posix_time::time_duration(0,
0, 0));
 
 
boost::posix_time::ptime end(boost::gregorian::date(2015, 4, 24),
boost::posix_time::time_duration(11,
59, 59));
 
boost::posix_time::time_period period(start, end);
 
if( period.contains(start) )
{
std::cout << "Start is Inclusive\n";
}
else
{
std::cout << "Start is Exclusive\n";
}
 
if( period.contains(end) )
{
std::cout << "End is Inclusive\n";
}
else
{
std::cout << "End is Exclusive\n";
}
}
 
and the output says the start is inclusive and the end is exclusive.
Seems very odd to me. Was there something wrong with my thinking?
 
Also, is there some alternative in the standard, and is that standard
available in msvc11?
 
 
 
 
 
 
 
 
 
 
 
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
scott@slp53.sl.home (Scott Lurndal): Apr 24 08:24PM


>The problem I am trying to solve is that I need to determine if any
>given datetime falls within a certain scheduled week.
 
[code elided]
 
>and the output says the start is inclusive and the end is exclusive.
>Seems very odd to me. Was there something wrong with my thinking?
 
You programmed a duration of 11:59:59. Consider the start
time as zero, then the time 11:59:59 is one second beyond
the end of the interval.
"Öö Tiib" <ootiib@hot.ee>: Apr 23 10:57PM -0700

> > advantages but there are several things that are tricky to do
> > header-only for non-template.
 
> What tricky things do you mean?
 
For example it is tricky to define static data members of class
for non-template (or full specialization) in header-only manner.
Knowledge about value of static data member may help compiler to
optimize something.
 
> > writer) in current C++. "Reflection" and "Concepts" might
> > help ... but these are only future plans right now.
 
> Are modules getting lost in the shuffle? I hope not.
 
What shuffle? I was referring to some obscure preprocessor and template
usages that can be hopefully get rid of with reflection or concepts.
scott@slp53.sl.home (Scott Lurndal): Apr 24 01:29PM


>> >In particular I'm interested in a real optimization - not just an ability to. Although knowing something that can be optimized but is not yet supported by compilers would be useful too.
 
>> You may also wish to consult the dragon book.
 
>not looking for theory - just an answer to specific question.
 
Ah, but then someone must, for free, spend the time to
construct an example that will illustrate sufficiently for
someone not versed in compiler technology. Who has the time?
woodbrian77@gmail.com: Apr 24 07:54AM -0700

On Thursday, April 23, 2015 at 4:58:44 PM UTC-5, Alain Ketterlin wrote>
 
> view on the question). But this does not mean that it is easy to find
> something valuable to add...
 
> -- Alain.
 
+1
 
Brian
Ebenezer Enterprises
http://webEbenezer.net
"A. Bolmarcich" <aggedor@earl-grey.cloud9.net>: Apr 24 02:48PM -0500

> the code generation phase. It is much easier to apply
> transformations to the intermediate representation than it
> is to [re]optimize already generated object code.
 
True. However, is it possible to do those types of optimizations when
linking. A compiler can put the intermediate representation in the
object file, in a section separate from the traditional text section.
Doing such optimizations when linking can be time comsuming, but the
resulting optimization of an exeuctable as a whole may be worth it.
Melzzzzz <mel@zzzzz.com>: Apr 24 05:08AM +0200

On 24 Apr 2015 01:45:45 GMT
> >Do you know a C++-Implementation for Windows that can
 
> For example, on April 22, 2015 GCC 5.1 was released.
> Is there a Windows executable available?
 
It's too early, gcc 5.1 didn't hit my repo yet ;p
 
 
> I do not need to access special Windows features,
> just command-line I/O would be fine.
 
[bmaxa@maxa-pc examples]$ g++-trunk -Wall -std=c++14 -O3 -march=native -pthread should.cpp -o should
should.cpp: In function 'int main()':
should.cpp:15:19: error: unable to find string literal operator 'operator""s' with 'const char [8]', 'long unsigned int' arguments
::std::cout <<( "example"s ).find( 'x' ) << '\n';
 
[bmaxa@maxa-pc examples]$ /home/bmaxa/clang/bin/clang -Wall -std=c++1y -O3 -march=native -pthread should.cpp -o should
should.cpp:15:28: error: no matching literal operator for call to 'operator "" s' with arguments of types 'const char *' and 'unsigned long', and no matching literal operator template
::std::cout <<( "example"s ).find( 'x' ) << '\n';
Melzzzzz <mel@zzzzz.com>: Apr 24 05:37AM +0200

On 24 Apr 2015 03:25:45 GMT
 
> int main()
> { using namespace ::std::string_literals;
> ::std::cout <<( "example"s ).find( 'x' ) << '\n'; }
 
Now it compiles fine.
Robbie Hatley <see.my.sig@for.my.address>: Apr 24 05:33AM -0700

On 4/23/2015 6:24 PM, Stefan Ram wrote:
 
> Is this a C++ that should be able to be compiled
> without compilation errors by a C++ compiler?
 
No. But see below.
 
> ::std::regex pat {R"(\w{2}\s*\d{5}(-\d{4})?)"};
> ::std::cout <<( "example"s ).find( 'x' ) << '\n';
> ::std::thread t( alpha ); t.join(); }
 
Has errors. (see below)
 
> execute this program (possibly after typos were corrected)?
> I am looking for a »precompiled binary« or windows
> installer, not a source tree.
 
Get Cygwin. It's a free development platform for Windows
which gives Windows a Linux-like feel (replaces cmd with
Bash for one thing, and re-interprets your file system
so that the Cygwin folder is / and the rest of your C: drive
is /cygdrive/c ). It has available compilers and interpreters
for a wide variety of programming languages: C, C++, Perl,
Python, Ruby, Haskel, and many more.
 
But your program has problems, even after I cleaned up some
of the more obvious errors (such as loads and loads of
superfluous colons):
 
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <regex>
#include <thread>
#include <memory>
void alpha(void) {
std::cout << "alpha" << std::endl;
}
int main(void)
{
std::unique_ptr<std::string> s =
std::make_unique<std::string> ("string");
std::regex pat {R"(\w{2}\s*\d{5}(-\d{4})?)"};
std::cout << ("examples").find('x') << std::endl;
std::thread t(alpha);
t.join();
return 0;
}
 
Compilation attempt with g++ gives these errors:
 
%g++ -Wall -std=c++11 program.cpp -o program.exe
 
weird-program.cpp: In function 'int main()':
weird-program.cpp:16:7: error: 'make_unique' is not a member of 'std'
std::make_unique<std::string> ("string");
^
weird-program.cpp:16:35: error: expected primary-expression before
'>' token
std::make_unique<std::string> ("string");
^
weird-program.cpp:18:30: error: request for member 'find' in
'("examples")', which is of non-class type 'const char [9]'
std::cout << ("examples").find('x') << std::endl;
^
 
Hmmm. Function "make_unique" *should* be in header <memory>.
Ah, I see, I'm compiling with std=c++11, but I need std=c++14.
 
Then you need to rethink your usage of "find" a if it were
a method of a string literal. String literals don't have
methods. But std::string does, so i'll make that correction
for you. Yes, THIS version compiles:
 
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <regex>
#include <thread>
#include <memory>
void alpha() {
std::cout << "alpha\n";
}
int main(void)
{
std::unique_ptr<std::string> s =
std::make_unique<std::string>("string");
std::regex pat {R"(\w{2}\s*\d{5}(-\d{4})?)"};
std::cout << std::string("examples").find('x') << std::endl;
std::thread t(alpha);
t.join();
return 0;
}
 
%g++ -Wall -std=c++14 program.cpp -o program.exe
 
[no errors, no warnings]
 
On running, prints:
 
1
alpha
 
 
 
--
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
https://www.facebook.com/robbie.hatley
Melzzzzz <mel@zzzzz.com>: Apr 24 03:13PM +0200

On Fri, 24 Apr 2015 05:33:58 -0700
> }
 
> Compilation attempt with g++ gives these errors:
 
> %g++ -Wall -std=c++11 program.cpp -o program.exe
 
You need -std=c++14 switch !
"K. Frank" <kfrank29.c@gmail.com>: Apr 24 08:59AM -0700

Hi Stefan!
 
On Thursday, April 23, 2015 at 9:24:29 PM UTC-4, Stefan Ram wrote:
> Is this a C++ that should be able to be compiled
> without compilation errors by a C++ compiler?
 
Yes, I can get it to work with the mingw-w64 version
of g++ 4.9.2. See comments in line and below.
 
 
> void alpha() { ::std::cout << "alpha\n"; }
 
> int main()
> { ::std::unique_ptr< ::std::string >s =
 
As you noted in your follow-up posting, I needed
to add
 
using namespace ::std::string_literals;
 
Thus,
 
int main()
{
using namespace ::std::string_literals;
::std::unique_ptr< ::std::string >s =
 
> execute this program (possibly after typos were corrected)?
> I am looking for a »precompiled binary« or windows
> installer, not a source tree.
 
Yes, I did this with a pre-built mingw-w64 compiler I
downloaded last year:
 
x86_64-4.9.2-release-posix-seh-rt_v3-rev0.7z
 
C:\>g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 4.9.2
 
I successfully compiled your program as follows:
 
C:\>g++ -std=c++14 -o test test.cpp
 
(If I "downgrade" to "-std=c++11" it complains about
"string_literals" and "make_unique". Note, "-std=gnu++14"
also works.)
 
By the way, when I run your program it gives the following
output:
 
1
alpha
 
This is on 64-bit windows 7.
 
 
Happy Hacking!
 
 
K. Frank
Robbie Hatley <see.my.sig@for.my.address>: Apr 24 04:58AM -0700

On 4/13/2015 10:35 AM, Doug Mika wrote:
 
> Hi to all, I was wondering what overloading the operator() is?
 
That's called an "application operator". If you define one for
a struct or class, that struct or class can then function as
a "functor" (function-like class). If you instantiate an object
of the class, you can then use that object as if it was a function.
 
Compile and run the following to see what I mean:
 
 
#include <iostream>
struct SayHi
{
void operator() (void)
{
std::cout << "Hello, World!" << std::endl;
}
};
int main (void)
{
SayHi Greeter;
Greeter();
return 0;
}
 
 
--
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
https://www.facebook.com/robbie.hatley
Victor Bazarov <v.bazarov@comcast.invalid>: Apr 24 08:06AM -0400

On 4/24/2015 7:58 AM, Robbie Hatley wrote:
> Greeter();
> return 0;
> }
 
To the OP:
 
Also note, that if the op() function does not change the object for
which it is called, it's better to declare it 'const':
 
void operator()() const
 
. Also, refrain from putting 'void' where nothing is needed (I mean the
argument list). It's easier to read and understand. If nothing is
supposed to be there, nothing needs to be declared there. Having 'void'
to designate an empty argument list is a C-ism.
 
BTW, another way of achieving the same effect would be to instantiate
and use the object in the same expression. The 'main' function in
Robbie's example might therefore be rewritten as
 
int main()
{
SayHi()();
}
 
V
--
I do not respond to top-posted replies, please don't ask
ram@zedat.fu-berlin.de (Stefan Ram): Apr 23 10:36PM

People told me that
 
::std::string s( ... )
 
was »direct initialization« and
 
::std::string s = ...
 
was »copy initialization«.
 
Herb Sutter wrote »Copy initialization means the object is
initialized using the copy constructor«.
 
I believe that in the case
 
::std::string t;
::std::string s( t );
 
, the object »s« is initialized using the copy constructor.
So then this is copy initialization, even if it uses
parentheses?
ram@zedat.fu-berlin.de (Stefan Ram): Apr 23 11:22PM

>declare and define a variable x using the default
>constructor, that is, there is not difference in the
>effect/meaning of »T x;« and »T x{};«?
 
Now I have observed a difference!
They differ, when T is a mere aggregate!
 
#include <initializer_list>
#include <iostream>
#include <ostream>
 
struct T { int i; };
 
int main()
{ T t; /* i is not initialized */
T t1{}; /* i is value-initialized */
::std::cout << t.i << '\n';
::std::cout << t1.i << '\n'; }
ram@zedat.fu-berlin.de (Stefan Ram): Apr 24 01:24AM

Is this a C++ that should be able to be compiled
without compilation errors by a C++ compiler?
 
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <regex>
#include <thread>
#include <memory>
 
void alpha() { ::std::cout << "alpha\n"; }
 
int main()
{ ::std::unique_ptr< ::std::string >s =
::std::make_unique< ::std::string >( "string" );
::std::regex pat {R"(\w{2}\s*\d{5}(-\d{4})?)"};
::std::cout <<( "example"s ).find( 'x' ) << '\n';
::std::thread t( alpha ); t.join(); }
 
Do you know a C++-Implementation for Windows that can
execute this program (possibly after typos were corrected)?
I am looking for a »precompiled binary« or windows
installer, not a source tree.
ram@zedat.fu-berlin.de (Stefan Ram): Apr 24 01:45AM

>Do you know a C++-Implementation for Windows that can
 
For example, on April 22, 2015 GCC 5.1 was released.
Is there a Windows executable available?
 
I do not need to access special Windows features,
just command-line I/O would be fine.
ram@zedat.fu-berlin.de (Stefan Ram): Apr 24 03:25AM

>::std::cout <<( "example"s ).find( 'x' ) << '\n';
 
I forgot a »using namespace«!
 
#include <iostream>
#include <ostream>
#include <string>

int main()
{ using namespace ::std::string_literals;
::std::cout <<( "example"s ).find( 'x' ) << '\n'; }
ram@zedat.fu-berlin.de (Stefan Ram): Apr 24 11:40AM

>as well as in new expressions (5.3.4), static_cast expressions (5.2.9),
>functional notation type conversions (5.2.3), and base and member
>initializers (12.6.2) is called direct-initialization."
 
Yes, but this does not exclude the possibility that »s( t )« above
might be both direct-initialization /and/ copy initialization.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: