2014-02-17

2014-01-pre-Issaquah mailingの簡易レビュー Part 1






SG10SG10CStudy GroupC

N3824: make_array

std::array<T>make_array使
// make_arrayの利用例
std::array<int, 3> a = std::make_array( 1, 2, 3 ) ;
std::array<long, 2> b = std::make_array( 1, 2L ) ;

std::common_type使narrowing conversion
// narrowing conversionは違法になる
auto a = std::make_array( 1, 2U ) ; // エラー


// 明示的に型を指定する例
auto a = std::make_arrary<long>( 1, 2U ) ; // OK

auto使
// make_arrayの利用例
auto a = std::make_array( 1, 2, 3 ) ;
auto b = std::make_array( 1, 2L ) ;

[PDF] N3825: SG13 Graphics Discussion

C++14使

C++C++使C++使

使使

SVG+CanvasC++使

cairoC++使

cairocairoCconst使

cairocairommC++cairomm2010

cairocairoC++cairoISO使調

C++14

N3827: Working Draft Technical Specification - URI

URI

[PDF] N3829: apply() call a function with arguments from a tuple (V2)

tupleapply



N3830: Scoped Resource - Generic RAII Wrapper for the Standard Library

RAIIscoped_resource

RAIIResource Acquisition Is InitializationC++
// RAIIの例
class raii_holder
{
    int * owned ;
public :
    explicit raii_holder( int * ptr )
        : owned( ptr ) { }

    raii_holder( raii_holder const & ) = delete ;    
    raii_holder & operator =( raii_holder const & ) = delete ;

    int * get() { return owned ; } 

    ~raii_holder()
    {
        delete owned ;
    }
} ;


int main()
{
    raii_holder r( new int(0) ) ;
    *r.get() = 123 ;

// 自動的にdeleteされる
} 

unique_ptrunique_ptr

RAIIC++14RAII
// 提案中のライブラリ
int main()
{
    auto r = std::make_scoped_resource( &::operator delete, new int(0) ) ;

// 自動でdeleteされる。
}


// System defined APIs:
void* VirtualAlloc(
void *pAddress,
size_t size,
unsigned int fAllocationType,
unsigned int fProtect);

bool VirtualFree(
void *pAddress,
size_t size,
unsigned int uiFreeType);

scoped_resouceVirtualFree
// VirtualFreeにもらくらく対応
int main()
{
    auto r = std::make_scoped_resource( &VirtualFree,
        VirtualAlloc( nullptr, PAGE_SIZE, MEM_COMMIT, PAGE_HARDWARE ),
        0,
        MEM_RELEASE ) ;

// 自動的にVirtualFree
}

Variadic Templates使

0
// 0個のリソースを管理する例
int main()
{
    auto r = std::make_scoped_resource(
        []{ std::cout << "I'm exception safe. Thank you very much." << '\n' ; }
    ) ;

// 自動的にlambda式が呼ばれる
}

r



unique_ptrscoped_resource使

[PDF] N3831: Language Extensions for Vector level parallelism

Clik PlusOpenMPSIMD

SIMDcountable loop

SIMD loopfor
// SIMD loop
for simd ( int i = 0 ; i < 10 ; ++i ) 
{
// 処理
}

simd使

for使
// SIMD forの条件式で使える式
relational-expression < shift-expression
relational-expression > shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression
equality-expression != relational-expression

for3+=, -=, E1 = E2 + E3, E1 = E2 - E3使使

return, break, goto, switch使

SIMDsimd
// SIMD関数の例
int f() simd { }
[]() simd { } ; // もちろんlambda式にも使える

simd

[PDF] N3832: Task Region

C++strict fork-join

C++使使

task_regiontask_runjoin



[PDF使] N3839: Proposing the Rule of Five, v2





C++11

C++11defaultdeprecatedC++11

C++14調5default
// 五原則の例
struct X
{
    ~X() { }
} 

int main()
{
    X x1 ;
    X x2 = x1 ; // エラー、五原則
}

C++deprecatedconst15
// 文字列リテラルからconst性をなくす例
int main()
{
    // well-formed in C++03
    // ill-formed in C++11
    char * ptr = "hello" ; 
}

C++14deprecated

[PDF] N3840: A Proposal for the World's Dumbest Smart Pointer, v3

observer_ptr


// observer_ptrの例
#include <memory>

int main()
{
    // std::observer_ptr<int> ptr = new int(0) ; と同じ
    auto ptr = std::make_observer( new int(0) ) ;

    auto p2 = ptr ; // コピーできる

    delete ptr ;
}

observer_ptr

使

[PDF使] N3841: Discouraging rand() in C++14

std::random_shufflestd::rand

std::rand使使
// 間違ったサイコロの実装
int roll( )
{
    return std::rand()%6 + 1 ;
}

std::rand使

std::random_shufflestd::rand

rand使

std::randstd::random_shuffle使std::randC++11使std::random_shuffleC++11std::shuffle使

[PDF] N3842: A sample Proposal

sampleKnuthThe Art of Computer Programming, Volume 2S(selection sampling technique) R(reservoir sampling)

vol. 2BC++11<random>std::knuth_b

S(Selection sampling)Forward iteratorOutput Iterator

R(Reservoir sampling)Input IteratorRandom Access Iterator

SR

template< class PopIter, class SampleIter, class Size, class URNG >
SampleIter
sample( PopIter first, PopIter last, SampleIter out, Size n, URNG&&g)
{
    using pop_t = typename iterator_traits<PopIter>::iterator_category;
    using samp_t = typename iterator_traits<SampleIter>::iterator_category;
    return __sample( first, last, pop_t{},
                     out, samp_t{},
                     n, forward<URNG>(g) );
}

KnuthRSGISTLsampleC++Rsample

[PDF] N3843: A SFINAE-Friendly std::common_type
[PDF] N3844: A SFINAE-Friendly std::iterator_traits

std::common_typestd::iterator_traitsSFINAE

C++11
// C++11では違法
using type = std::common_type<int, int *>::type ;

intint *

SFINAE
// SFINAEとはならない例

// 共通の型がある場合のみ候補に上がるべき関数テンプレート
template < typename T1, typename T2,
    typename COMMON_TYPE = std::common_type<T1, T2>::type >
void f( T1, T2 ) { }

// その他の関数fのオーバーロード

int main()
{
    int a1 = 0 ;
    int * a2 = nullptr ;

    f( a1, a2 ) ; // コンパイルエラー

} 

SFINAE使fcommon_typeill-formed

N3843ill-formedSFINAE使std::common_type

std::result_typeSFINAE

N3462: std::result_of and SFINAE

result_typecommon_typeiterator_traitsSFINAE

 (SubaruG)



A Blog Post

Although written in Japanese, the author shows an invoke function defined with result_of that fails to compile because it (result_of) is not SFINAE-friendly. It also calls out std::common_type as another trait that would benefit from being SFINAE-friendly.




FDIS  - C++er

 std::result_of  std::common_type  SFINAE 使


[PDF] N3845: Greatest Common Divisor and Least Common Multiple

gcdlcm<cstdlib>



gcdlcmconstexpr

gcdlcmconstexprabs


// N3845のリファレンス実装例
// 仮にclib名前空間内に置く
namespace clib {

// 任意の整数型で動くテンプレート版のabs
template< class T >
constexpr auto abs( T i ) -> enable_if_t< is_integral<T>{}(), T >
{
    return i < T(0) ? -i : i; }
}

// 二つの実引数が整数型であるかどうかを確認し、また共通の型を返すメタ関数common_int_t
template< class M, class N = M >
using common_int_t = enable_if_t< is_integral<M>{}() and is_integral<N>{}()
, common_type_t<M,N>
> ;

// 最大公約数
template< class M, class N >
constexpr auto gcd( M m, N n ) -> common_int_t<M,N>
{
using CT = common_int_t<M,N>;
return n == 0 ? clib::abs<CT>(m) : gcd<CT,CT>(n, m % n);
}

// 最小公倍数
template< class M, class N >
constexpr auto lcm( M m, N n ) -> common_int_t<M,N>
{ return abs((m / gcd(m,n)) * n); }

使使

[PDF] N3846: Extending static_assert

C++11static_assert
// C++11のstatic_assertの例
static_assert( true, "This can't be possible!") ;

assert

BOOST_STATIC_ASSERTstatic_assert
// BOOST_STATIC_ASSERTの実装
#define BOOST_STATIC_ASSERT(B) static_assert(B, #B)


BOOST_STATIC_ASSERT(( sizeof(int) == 4 )) ;


static_assert(( sizeof(int) == 4 ), "( sizeof(int) == 4 )") ;

static_assert便

static_assertstatic_assert

BOOST_STATIC_ASSERT

C++Phase of Translation

C++

C

static_assertstatic_assertstatic_assert
#define STATIC_ASSERT(B) static_assert(B, #B)


#define STATIC_ASSERT(B) static_assert(B, "ouch")





static_assertprintf






1 (Daniel Krügler)


diagnostic

BOOST_STATIC_ASSERT


2 (Mike Miller)


diagnostic




4





// 案4の利用例
static_assert( sizeof(int) == 4,
    "Oops, the size of int is not 4! Use this:", sizeof(int),
    " Also check the size of long, just in case :D :", sizeof(long)
) ; 





static_assert

[PDF] N3847: Random Number Generation is Not Simple!

使std::pick_a_number

pick_a_number
// N3847提案のpick_a_number
#include <random>

int main()
{
    int a = std::pick_a_number( 1, 6 ) ;
    double b  = std::pick_a_nubmer( 1.0, 6.0 ) ;
}

<algorithm>

copy_n



N3848: Working Draft, Technical Specification on C++ Extensions for Library Fundamentals

std::optional

N3849: string_view: a non-owning reference to a string, revision 6

string_view

std::stringnullchar

GoogleBloombergChromiumLLVM

string_viewstd::basic_stringstring_viewLLVM

[PDF] N3850: Working Draft, Technical Specification for C++ Extensions for Parallelism

<algorithm>

(execution policy)

std::seq, std::par, std::vec(sequential execution policy)(parallel execution policy)(vector execution policy)

使


// N3850の例
#include 

int main()
{
    std::vector v ;
    // vに値を入れる

    // 従来のsort、暗黙にシーケンシャル実行ポリシー
    std::sort( v.begin(), v.end() ) ;

    // 明示的にシーケンシャル実行ポリシー
    std::sort( std::seq, v.begin(), v.end() ) ;

    // 並列実行ポリシー
    std::sort( std::par, v.begin(), v.end() ) ;

    // ベクトル実行ポリシー
    std::sort( std::vec, v.begin(), v.end() ) ;
}



[PDF] N3851: Multidimensional bounds, index and array_view

array_viewMicrosoftC++ AMP

array_view
// N3851時点での提案の例
std::size_t M = 32 ;
std::size_t N = 64 ;

std::vector<float> v( M * N ) ;

std::array_view<float, 2> view{ { M, N }, v } ;


view[{ 3, 3 }] = 0 ;

array_viewbounds_iterator
// N3851時点でのイテレーター
std::bounds_iterator<2> first = std::begin( view.bounds() ) ;
std::bounds_iterator<2> last = std::end( view.bounds() ) ;

float sum = 0.0 ;

std::for_each( first, last,
[]( std::index<2> index )
{
    sum += view[index] ;
}

string_viewarray_viewmutableLLVM20112immutableArrayRefmutableMutableArrayRef20131StringRefMutableStringRef

N3852: C++ FCD Comment Status

National Body CommentC++

NB

CH2: C++14optionaldynarrayTSTechnical Specification

US15: N3655typetrait<...>::type使typetrait_t<...>

NB
// 数値区切りの例
int x = 1000'000'000 ;

ES8: operator delete[]std::size_t

GB4: core issue 1786

ES10, US8: [[deprecated]]CDN3394CD

US9: 0C99C++14C990G++0core issue 1768

CH5: __func__lambdalambda使

US13: volatilereturn

ES11: forward_as_tupleconstexprconstexpr

GB9: C11getsC++C99getsC++

FI4: 
// 変換関数に関数の戻り値の型推定を適用する例
struct Zero
{
    operator auto() { return 0 ; }
} ;

int main()
{
    Zero zero ;
    int x = zero ;
}

N3853: Range-Based For-Loops: The Next Generation

range-based for loopfor使
// 昔のforループ

void once_upon_a_time( std::vector<int> & range )
{
    for (   std::vector<int>::iterator first = range.begin(),
            std::vector<int>::iterator last = range.end() ;
            first != last ; ++first )
    {
        std::cout << *first << '\n' ;
    }
}

// 今のforループ

void and_now( std::vector<int> & range )
{
    for ( int elem : range )
    {
        std::cout << elem << '\n' ;
    }
}


// auto specifierの利用
for ( auto elem : range ) ;


// Range-based for loopとauto specifierの問題点

void f( std::vector< std::string > & range )
{
    for ( auto elem : range )
    {
        std::cout << elem << '\n' ;
    }
}

std::stringC++


// コピーが発生してしまうコード
void f( std::map< const int, int > & range )
{
    for ( std::pair< int, int > const & elem : range )
    {
    // ...
    }
}

rangestd::pair< const int, int >

auto

"for ( auto & elem : range )"vector<bool>
// プロクシーに対応できない例
void f( std::vector<int> &v)
{
    for ( auto & elem : v )
    {
        std::cout << elem << '\n' ;
    }
}

int main()
{
    std::vector<int> non_proxy{ 1, 2, 3  } ;
    f( non_proxy ) ; // well-formed

    std::vector<bool> proxy{ true, true, true } ;
    f( proxy ) ; // ill-formed!
} 

vector<bool>使bool1char1bit

bool

"for( auto const & elem : range )"

"for ( auto && elem : range )"

auto &&

auto &&使rvalue referenceauto specifierTemplate Argument Deductionrvalue reference

使使使"for ( elem : range )"

"for ( elem : range )""for ( auto && elem : range )"使

N3854: Variable Templates For Type Traits

C++14type traits使
// 新旧比較
std::add_pointer<int>::type old = nullptr ;
std::add_pointer_t<int> latest = nullptr ;

::type

type_traits_t
template < typename T >
add_pointer_t = std::add_pointer<T>::type ;

C++14
// 新旧比較
bool old = std::is_integer<int>::value ;
bool latest = std::is_integer_v<int> ;

::value



template < typename T >
constexpr bool is_integer_v = is_integer<T>::value ;



[PDF] N3856: Unforgetting standard functions min/max as constexpr

PDF

min/maxconstexpr



[PDF] N3857: Improvements to std::future<T> and Related APIs

future便N3858

future使
// C++11のfuture
#include <future>

int main()
{
    auto f = std::async( [](){ return 123 ; } ) ;

    auto result = f.get() ;
} 

futurefuturegetgetfuturepromise

then

futurefutureC++11
// C++11の例
#include <future>

int main()
{
    // まあ、これはいいか
    auto f1 = std::async( [](){ return 123 ; } ) ;
    auto r1 = f.get() ;// え、俺がやるの?

    // またかよ・・・
    auto f2 = std::async( [&](){ return r1 + 456 ; } ) ;
    auto r2 = f2.get() ; // で、また俺がやるの?
} 



then
// then
#include <future>

int main()
{
    auto f2 = std::async( []() { return 123 ; } )
                .then( []( auto f ){ return f.get() + 456 ; }
              )
    auto r2 = f2.get() ;   
} 

thenfuturefuture.then(..).then(...).then(...)

unwrap

futurefuture<future<int>>>futureget使future

unwrapfuture
// unwrapの例
#include <future>

int main()
{
    // outer_futureはstd::future< std::future< int > >
    auto outer_future = std::async( []{
        return std::async( [] {
            return 123 ;
        } ) ; // inner
    } ) ; // outer

    auto inner_future_proxy = outer_future.unwrap() ;

    inner_future_proxy.then([]( auto f ) {
        auto result = f.get() ;
    } ) ;
} 

is_ready

C++11futuregetgetC++11is_ready
// is_readyの例
#include <future>

void do_something_or_other( std::future<int>f)
{
    if ( f.is_ready() )
    {
        int value = f.get() ; // ブロックしない
    }
    else
    {
        // getはブロックするので、なにか別のことをする
    }
}

wait_forwait_until

when_any

futurefuture

2future<vector<future<T>>>future<tuple<future<T>>>futurevectortuple
// when_anyの例
#include <future>

int main()
{
    std::future<int> futures[] = { 
        std::async( []{ return 1 ; },
        std::async( []{ return 2 ; },
        std::async( []{ return 3 ; }
    } ;

    // std::future< std::vector< std::future<int> > >
    auto vec_future = std::when_any( std::begin(futures), std::end(futures) ) ;
    // std::future< std::tuple< std::future<int>, std::future<int>, std::future<int> > >
    auto tuple_future = std::when_any( futures[0], futures[1], futures[2] ) ;

    vec_future.then([]{
        // どれかが完了している
    } ) ;
} 

when_all

futurefuture

make_ready_future

futurefutureC++11promisepromisepromisefuture
// 値をセット済みのfutureを作る
template <typename T >
std::future< typename std::decay_t<T> > make_ready_future( T && value )
{
    std::promise< typename std::decay<T>::type > p ;
    p.set_value( std::forward<T>( value ) ) ;
    return p.get_future() ;
}



[PDF] N3858: Resumable Functions

Resumable Function

Resumable Functionfuturethenresumable function

future.then使
// future.thenの例
future<int> f(shared_ptr<stream> str)
{
    shared_ptr<vector<char>> buf = ...;
    return str->read(512, buf)
        .then([](future<int>op)
        // lambda 1
            {
                return op.get() + 11;
            });
}

future<void> g()
{
    shared_ptr<stream> s = ...;
    return f(s).then([s](future<int>op)
        {
            s->close();
        });
}


// resumable functionの例
future<int> f(stream str) resumable
{
    shared_ptr<vector<char>> buf = ...;
    int count = await str.read(512, buf);
    return count + 11;
}

future<void> g() resumable
{
    stream s = ...;
    int pls11 = await f(s);
    s.close();
}

resumable function

resumable function

resumable functionresumable使resumableresumable
// resumableキーワード
std::future<int> f() resumable ;
[]() resumable {} ;

resumable
// 関数のresumable指定子の位置
struct S
{
    std::future<int> f() & resumable noexcept ;
} ;

lambdamutable
// lambda式のresumable指定子の位置
[]() mutable resumable noexcept -> std::future<int>{ return 0 ; }

resumablelambdaTreturnstd::future<T>
// lambda式かつresumable functionの戻り値の型推定
// 戻り値の型はstd::future<int>
[]() resumable { return 0 ; }

resumable function


resumable functionstd::future<T>std::shared_future<T>Tvoidresumable function

C(...)使Variadic Templates使


resumable functionawait使
await expr

使await使awaitresumable
//awaitの例
std::future<void> f() resumable
{
    // 事前の処理
    int r1 =  await std::async( []{ return 0 ; } ) ;
    // 事後の処理
    int r2 = await std::async) [] { return 0 ; } ) ;
    // さらに処理

    return ;
}

await


awaitresumable functiondecltype使

awaitfuture<T>shared_future<T>

await使

mutexawait

awaitfutureshared_futuregetvoidawait使


resumable function

resumable functionresumable function





futureshared_futuregetthenis_ready

C#Python使yieldyield

便

[PDF] N3859: Transactional Memory Support for C++


(Transactional Memory)



mutex
// mutexの例
int x = 0 ;
int y = 0 ;
// x, yにアクセスする際は、かならずmをロックすること
std::mutex m ;

void f()
{
    {
        std::lock_guard< std::mutex > lock(m) ;

        ++x ;
        ++y ;
    }
}

mutex

Transactional Memory使
// N3859提案のTransactional Memoryの例
int x = 0 ;
int y = 0 ;

void f()
{
    synchronized
    {
        ++x ;
        ++y ;
    }
}

Transactional Memory使mutex

(synchronized block)synchronized

Transactional Memory(Atomic Block)
// N3859提案のAtomic Block三種類
atomic noexcept { /* ... */ }
atomic commit_except { /* ... */ }
atomic cancel_except { /* ... */ }

atomicnoexcept/commit_except/cancel_except

atomic noexcept
// atomic noexcept
atomic noexcept
{
    try
    {
        // ...
    }
    catch( ... )
    {
        // 絶対に外に例外を投げないように握りつぶす
    }
}

atomic commit_except
// atomic commit_except
int x = 0 ;
int y = 0 ;

void f()
{
    try
    {
        atomic commit_except
        {
            ++x ;
            throw 0 ;
            ++y ;
        }
    } catch( ... ) { }

    // この時点で、他のスレッドを考慮に入れなければ
    // xはインクリメントされている
    // yは初期値のまま
}

atomic cancel_exceptTransaction-safe
// atomic cancel_except
int x = 0 ;
int y = 0 ;

void f()
{
    try
    {
        atomic cancel_except
        {
            ++x ;
            throw 0 ;
            ++y ;
        }
    } catch( ... ) { }

    // この時点で、他のスレッドを考慮に入れなければ
    // xは初期値のまま
    // yは初期値のまま
}

transaction-safeN3859bad_alloc, bad_array_length, bad_array_new)length, bad_cast , bad_typeid , 


// 副作用は一斉に見えるか見えないか
int x = 0 ;
int y = 0 ;

void f()
{
    atomic noexcept
    {
        ++x ;
        // #1
        ++y ;
    }
    // #2
}

#1xx

#2xy

mutex

Transaction-safeTransactional Memorytransaction-safetransaction-unsafeSTLtransaction-safe使

[PDF] N3861: Transactional Memory (TM) Meeting Minutes 2013/09/09-2014/01/20



[PDF] N3862: Towards a Transaction-safe C++ Standard Library: std::list

GCC 4.9Transactional Memory使libstdc++std::listtransaction-safe

GitHub

mfs409/tm_stl: A scratchpad for working on making some of the C++11 STL code from gcc 4.9 transaction-safe


ドワンゴ広告

この記事はドワンゴの勤務時間中に書かれた。

ドワンゴは本物のC++プログラマーを募集しています。

採用情報|株式会社ドワンゴ

CC BY-ND 4.0: Creative Commons — Attribution-NoDerivatives 4.0 International — CC BY-ND 4.0

5 comments:

Anonymous said...





February 17, 2014 at 5:19PM
Anonymous said...


PDF


February 17, 2014 at 7:32PM
Anonymous said...




static_assert
template 
constexper (char&)[N] printf(const char[],const T&&...);

static_assert
VC
GDCLCMcmath





February 18, 2014 at 3:12AM
Anonymous said...


3

int N,class T...

GDCGCD




February 18, 2014 at 3:15AM
Anonymous said...


PDF