読者です 読者をやめる 読者になる 読者になる

C++0x いきなりつまづくコンセプト

C++

ConceptGCC 4.3.0 Alpha 7 では以下のコードが通らない

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

template <std::Range Rng>
void sort(Rng& r)
{
    std::sort(begin(r), end(r)); // エラー
}

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

    v.push_back(3);
    v.push_back(1);
    v.push_back(4);

    sort(v);

    for (int item : v) {
        std::cout << item << std::endl;
    }
}


このコードで ConceptGCC が吐き出すエラーメッセージは以下

main.cpp: In function 'void sort(Rng&)':
main.cpp:8: error: no matching function for call to 'sort(std::Range<Rng>::iterator&, std::Range<Rng>::iterator&)'
/opt/conceptgcc-4.3.0-alpha-7/lib/gcc/i686-pc-cygwin/4.3.0/../../../../include/c++/4.3.0/bits/stl_algo.h:2677: note: candidates are: void std::sort(_Iter, _Iter) [with _Iter = iterator'] <requirements>
main.cpp:8: note:   no concept map for requirement 'std::RandomAccessIterator<std::Range<Rng>::iterator>'
main.cpp:8: note:   you may need to add the requirement 'std::RandomAccessIterator<std::Range<Rng>::iterator>' to the current requirements clause
main.cpp:8: note:   no concept map for requirement 'std::OutputIterator<iterator>', std::Iterator<std::Range<X>::iterator>::reference>'
main.cpp:8: note:   you may need to add the requirement 'std::OutputIterator<iterator', std::Iterator<std::Range<X>::iterator>::reference>' to the current requirements clause
main.cpp:8: note:   no concept map for requirement 'std::OutputIterator<iterator>', const std::IteratorBase<std::Range<X>::iterator>::value_type&>'
main.cpp:8: note:   you may need to add the requirement 'std::OutputIterator<iterator', const std::IteratorBase<std::Range<X>::iterator>::value_type&>' to the current requirements clause
main.cpp:8: note:   no concept map for requirement 'std::LessThanComparable<std::IteratorBase<std::Range<X>::iterator>::value_type>'
main.cpp:8: note:   you may need to add the requirement 'std::LessThanComparable<std::IteratorBase<std::Range<X>::iterator>::value_type>' to the current requirements clause
main.cpp:8: note:   no concept map for requirement 'std::ShuffleIterator<iterator'>'
main.cpp:8: note:   you may need to add the requirement 'std::ShuffleIterator<iterator'>' to the current requirements clause
main.cpp:8: note:   no concept map for requirement 'std::CopyConstructible<std::IteratorBase<std::Range<X>::iterator>::value_type>'
main.cpp:8: note:   you may need to add the requirement 'std::CopyConstructible<std::IteratorBase<std::Range<X>::iterator>::value_type>' to the current requirements clause

std::Range::iterator が RandomAccessIterator, OutputIterator, LessThanComparable, ShuffleIterator, CopyConstructible の要求を満たさないと言われているがなぜなのかさっぱりわからない



追記:

std::Container コンセプトを使用した場合は以下のようになる

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

template <std::Container Cont>
void sort(Cont& c)
{
    std::sort(begin(c), end(c)); // エラー!
}

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

    v.push_back(3);
    v.push_back(1);
    v.push_back(4);

    sort(v);

    for (int item : v) {
        std::cout << item << std::endl;
    }
}
main.cpp: In function 'void sort(Cont&)':
main.cpp:8: error: invalid use of incomplete type 'concept std::Container<X>'/opt/conceptgcc-4.3.0-alpha-7/lib/gcc/i686-pc-cygwin/4.3.0/../../../../include/c++/4.3.0/bits/iterator_concepts.h:191: error: declaration of 'concept std::Container<X>'
main.cpp:8: error: invalid use of incomplete type 'concept std::Container<X>'/opt/conceptgcc-4.3.0-alpha-7/lib/gcc/i686-pc-cygwin/4.3.0/../../../../include/c++/4.3.0/bits/iterator_concepts.h:191: error: declaration of 'concept std::Container<X>'

std::Container が不完全型だと言われています。

う〜む・・・