Rokiのチラ裏

学生による学習のログ

Clang 3.1 range-based forにてassociated namespaceにstd namespaceを加えてADLする

大分前の話なので2016年の末に取り上げる話題ではないだろうが、メモ。

ツイート内でテストコードへのリンクがあるが一応貼る事にする。

#include<utility>
typedef int* exp_incrementable_t;

namespace nm{
    struct X{};
}

namespace std{
    constexpr exp_incrementable_t begin(const nm::X&){return nullptr;}
    constexpr exp_incrementable_t end(const nm::X&){return nullptr;}
}

int main()
{
    nm::X x;
    for(auto&& a:std::move(x))a;
}

このコードはC++11でも現在ill-formedであるがClang 3.1では通る。しかしこれはコンパイラのバグではなく、http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1442で確認できる通り、associated namespaceにstd namespaceを加えるという文言を削除している事に起因する差異である。尚、Clang 3.2以降は-std=c++11をオプションに付与したとしても当該文言が適用された状態で文脈解釈が成される。