Saturday, August 8, 2015

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

"K. Frank" <kfrank29.c@gmail.com>: Aug 08 04:27PM -0700

Hello Group!
 
The new "for" syntax is convenient for iterating
over collections:
 
for (auto x : collection) // do stuff with x;
 
But lots of times you want to give slightly special
treatment to the first or last element:
 
for (unsigned i = 0; i < collection.size(); i++) {
if (i == 0) // do something special;
if (i == 0) continue; // or don't do something
// do stuff with collection[i];
if (i == collection.size() - 1) // do something special;
}
 
As a concrete (and common) example, suppose you want
to print out a vector in parenthesized, comma-separated
notation:
 
(1, 2, 3, 4, 5)
 
Is there a nice idiom for this using "for (auto x : y)"?
 
You could always just introduce a counter:
 
unsigned i = 0;
for (auto x : collection) {
// print x
if (++i == collection.size()) break;
// print ", "
}
 
But by the time you do that, you might as well just
revert to the old-school "for (unsigned i = 0, ...)"
loop.
 
 
Thanks for any ideas.
 
 
K. Frank
Doug Mika <dougmmika@gmail.com>: Aug 08 02:37PM -0700

Could anyone tell me why the following won't compile? To be honest, I don't know where to begin with the compiler error messages, even the first one.
 
Thanks
 
// Example program
#include <iostream>
#include <string>
#include <future>
#include <type_traits>
 
template<typename F, typename A>
std::future<std::result_of<F(A&&)>::type> spawn_task(F&& f, A&& a){
typedef std::result_of<F(A&&)>::type result_type;
std::packaged_task<result_type(A&&)> task(std::move(f)));
std::future<result_type> res(task.get_future());
std::thread t(std::move(task),std::move(a));
t.detach();
return res;
}
 
int cube(int x){
return x*x*x;
}
 
int main()
{
int myInt=3;
std::future<int> result = spawn_task(cube, myInt);
cout<<"The result is: "<<result.get()<<endl;
}
Diego Martins <jose.diego@gmail.com>: Aug 08 12:19PM -0700

https://github.com/irrequietus/typestring
 
it seems pretty interesting :-)
not to be confused with constexpr string literals. there strings are real
types!
 
pasting the readme file:
 
Strings as C++ types for use in template parameter lists.
What this is all about
 
Template metaprogramming is for those who enjoy using a pure, non-strict, functional programming language, with pattern matching within C++. If there is anything truly worth in the language that's the completely serendipitous emergence of this important idiom to the point of it being a core practice for lots of really useful and advanced code. One thing that is currently missing is having the opportunity to seamlessly define strings as types in situ within a template parameter list as a language feature.
 
I have been using various personal implementations in my own code for this, though I think that it is such a fundamental construct it should also exist in stand-alone mode. This is a fast compile-time wise, single-header, no-dependencies, standard-compliant C++ library offering a seamless way to enjoy compile time strings as individual, distinct types.
 
typedef typestring_is("Hello!") test; /* irqus::typestring<'H','e','l','l','o','!'> */
If you don't realize what this is for, by now, then it is probably not meant for you. At some point, perhaps the language does get some decent language features doing such stuff in one go; the way this works is actually much faster than expected given the few lines of implementation in typestring.hh.
 
Usage
 
You only need to include the file typestring.hh in your code, after which, you will be able to use typestring_is easily for any kind of char encoded string you fancy, including ASCII art. The default maximum length of the strings allowed is 64, but you can use any power of 2 within 0 (1) to 10 (1024) by enabling -DUSE_TYPESTRING=<power of 2> prior to compilation. The actual procedure behind creating these compile-time strings as types for a template parameter list allows for far greater numbers, but you will just segfault your compiler, regardless of what instantiation depth you specify.
 
A few things to remember
 
Will work with standard C++11, C++14 and above as it is, no dependencies involved at all.
Completely independent of compile-time constexpr metaprogramming string literals; you have individual strings as individual types to use as type parameters.
Has been tested with clang++ and g++, use as recent versions as possible for C++11/C++14 compliance.
A few handy member functions have been added in typestring should you need to play with it outside its intended use.
Code could be made to do far more, but the fundamental construct is what matters, I have another project for such things.
Always remember to have fun first and foremost.
License is Mozilla Public Licence 2.0 because it suits all metaprogramming best.
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Aug 08 10:31AM +0100

On Sat, 08 Aug 2015 00:00:40 +0300
> {
> q1.emplace( [=]() { f(i); } );
> }
 
The other mechanism that the standard offers to construct closures is
std::bind. lambda expressions are generally easier to use though,
particularly for partial function application: partial function
application requires using std::placeholders with std:bind. (This issue
does not apply to std::packaged_task of course, which requires a fully
closed-over function object when constructed.)
 
Chris
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: