decltype(x)::memberはGCC 4.7から

decltypeした型のメンバを使用する機能は、GCCでは4.7から使用できるようになったようです。備忘記。

#include <iostream>
#include <map>
#include <boost/range/algorithm/for_each.hpp>

int main()
{
    const std::map<std::string, int> m = {
        {"Alice", 16},
        {"Bob", 32},
        {"Carol", 23}
    };

    boost::for_each(m, [](decltype(m)::const_reference x) {
        std::cout << x.first << ", " << x.second << std::endl;
    });
}
Alice, 16
Bob, 32
Carol, 23


GCC 4.7がリリースされました

GCC 4.7 Release Series Changes, New Features, and Fixes


遅くなってしまいましたが、2012/03/22(木)にGCC 4.7.0がリリースされました。
リリースノートからC++11周りの変更点を抜粋します。

  • C++11を使用可能にするためのオプションをこれまで-std=c++0x, -std=gnu++0x, -Wc++0x-compatを使用していたが、-std=c++11, -std=gnu++11, -Wc++11-compatを使用できるようにした。効果はこれまでと同じ。
  • 拡張friend宣言をサポート
  • override/finalキーワードをサポート
  • ユーザー定義リテラルをサポート
  • alias declaration, template aliasesをサポート
  • 移譲コンストラクタをサポート
  • atomicアクセスのC++11クラスをサポート
  • C++11を有効にした場合の__cplusplusマクロの値を201103Lにした
  • C++11のライブラリの至る所をnoexceptを使用するよう修正
  • pointer_traits, allocator_traits, scoped_allocator_adaptorをサポート
  • tupleのuses-allocatorによるコンストラクトをサポート
  • vectorを、アロケータの要件を満たすよう修正
  • monotonic_clockをsteady_clockに名前変更
  • スレッドライブラリをより多くの環境で使用可能にした
  • unordered連想コンテナのイテレータデバッグモードを付けた
  • 非staticデータメンバの初期化


それと、GCC拡張ですが、STMのための構文が使用可能になっています。
参照: GCC 4.7にはTransactional Memoryの拡張が入る予定


GCC 4.7にはTransactional Memoryの拡張が入る予定

Transactional Memory in GCC
gcc 4.7 adds Transactional Memory for c/c++ - reddit


GCCの言語拡張で、Transactional Memoryを書くための構文と属性が提供されるようです。

__transaction_atomic {
    c = a - b;
}

コンパイラの恩恵が受けられると最適化に期待できるので、これはうれしい拡張ですね。



GCC 4.5から使用可能なC++0x機能

以下の4つです。

#include <iostream>
#include <vector>
#include <algorithm>

template <class T>
class smart_ptr {
    T* p_;
public:
    smart_ptr() : p_(0) {}

    explicit operator bool() const // 1.明示的な型変換演算子
    {
        return p_ != 0;
    }
};

int main()
{
    smart_ptr<int> p;
    if (p) {}
//  int x = p + 1; // NG

    const std::vector<int> v = {1, 2, 3};

    // 2.ラムダ式
    std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << std::endl; });

    // 3.ローカルクラスをテンプレート引数として使用
    struct local {};
    std::vector<local> l;

    // 4.Raw String Literal
    std::string path = R"[C:\a.txt]";
    std::cout << path << std::endl; // 「C:\a.txt」と出力される
}

constexprもこっそり入ってますが、キーワードがあるだけのようです。
十分な実装はできてるけどGCC 4.5には間に合わなかったそうです。(パッチはあるらしい)


追記:
3つではなく4つでした。
Raw String Literalを追記しました。



cygwinのGCCでBoostを使う

φ(..)メモメモ...

g++ -Wall -o sample main.cpp -std=gnu++0x -I "C:\Program Files\boost\boost_1_39_0"


なぜかうまくいかなかった例

g++ -Wall -o sample main.cpp -std=gnu++0x -I "/cygdrive/c/Program Files/boost/boost_1_39_0/"
g++ -Wall -o sample main.cpp -std=gnu++0x -I /cygdrive/c/progra~1/boost/boost_1_39_0/
g++ -Wall -o sample main.cpp -std=gnu++0x -I /cygdrive/c/Program\ Files/boost/boost_1_39_0/

GCC 4.4動いた!

ネタ元:blog.y4su0.com - gcc 4.3.1 を Windows で使ってみる


Windows用にビルドされたGCCのバイナリがあると聞いたのでインストールしてみました。

ここからgcc 4.4-20090123のsnapshot版をダウンロードして、インストーラを使ってインストール


とても簡単です。
(cygwinは事前インストールが必要cygwinをインストールしなくても、コマンドプロンプトだけで実行可能です)


まず言語仕様

#include <vector>

// Defaulted and Deleted Function
struct X {
    X() = delete;
    X(const X&) = default;
};

// Strongly-typed enums
enum class E : int { A = 0, B };

// inline namespace
namespace std {
    inline namespace tr1 {
        template <class T, int N>
        class array {};
    }
}

// 戻り値の型を後置する関数宣言構文(そのうち変わるかも)
template <class T, class U>
auto plus(T a, U b) -> decltype(a + b) { return a + b; }

int main()
{
    std::vector<int> v = {1, 2, 3}; // Initializer Lists

    const auto& a = v; // auto-typed variables

    // スコープがあるenum
    E e = E::A;

    // stdでもstd::tr1でもarrayにアクセス可
    std::array<int, 3> ar;
    std::tr1::array<int, 3> arr;

    int result = plus(1, 2);
}

動いた〜。


次にライブラリ


chrono

#include <chrono>

using namespace std::chrono;

int main()
{
    // 500ナノ秒待つ
    auto go = system_clock::now() + nanoseconds(500);
    while (system_clock::now() < go);
}


forward_list

#include <iostream>
#include <iterator>
#include <forward_list>
#include <algorithm>

using namespace std;

int main()
{
    forward_list<int> ls;

    ls.push_front(3);               // 先頭に3を追加
    ls.insert_after(ls.begin(), 1); // 先頭の後ろに1を追加

    size_t size = distance(ls.begin(), ls.end()); // 要素数取得

    copy(ls.begin(), ls.end(), ostream_iterator<int>(cout, "\n"));
}


Placement Insert

#include <vector>

struct hoge {
    hoge(int a, int b, int c) {}
};

int main()
{
    std::vector<hoge> v;

    v.emplace_back(1, 2, 3);
    v.emplace(v.begin(), 3, 2, 1);
}

使ってないけどとかもありますよ。



GCC 4.4のC++0x対応状況は以下を参照

Status of Experimental C++0x Support in GCC 4.4



ConceptGCCでコンパイルすると落ちる

ConceptGCC の新バージョンが出たと聞いたので

ここから「August 7, 2008 -- ConceptGCC 4.3.0 Alpha 7」の cygwin 版をダウンロードしてインストールして

PATH=/opt/conceptgcc-4.3.0-alpha-7/bin:${PATH}

conceptg++ main.cpp

とすると cc1plus.exe が落ちます。。。なぜだ・・・


ちなみに Vista x86 です


だれか助けて


GCC 4.3.0でC++0xコードのコンパイル

「-std=c++0x」のオプションを付けるとC++0xのコードがコンパイルできる


このオプションを指定しないと↓のようなコンパイルエラーになる
「error: ISO C++ does not include variadic templates」


右辺値参照とかだと「&&演算子なんか使えないよ」という感じのコンパイルエラーになる

c++ -std=c++0x -o sample main.cpp