如何使用 g++ 6.2.0 启用概念?

How to enable concepts with g++ 6.2.0?

这是我用来测试 c++ 概念功能的代码。然而,即使在 g++ 6.2.0 版本上使用标志 -fconcepts 后它似乎也不起作用。 任何让它工作的帮助都会很棒!

#include <iostream>
using namespace std;
#include <list>
#include <vector>
#include <algorithm>
#include <iterator>
#include <bits/stdc++.h>
using namespace std::literals;

template<typename ptr_t >
requires RandomAccessIterator<ptr_t> 
void mysort(ptr_t first, ptr_t last)
{
    sort(first, last);
}
int main()
{
    vector<int> v{22, 11, 55, 33, 44};
    list<int> l{22, 11, 55, 33, 44};
    mysort(begin(v), end(v));
    mysort(begin(l), end(l));
}

我是这样编译的:

g++-6 concepts.cpp -fconcepts

这是我得到的错误:

error: ‘RandomAccessIterator’ was not declared in this scope

我把拼写改成了random_access_iterator,但还是不行。

6.2.13 节中的文档 C++ Working Draft 定义了 RandomAccessIterator 的存在。

RandomAccessIterator 不是标准库提供的 (yet)。你需要定义它。

relevant documentation on cppreference 是了解概念要求的好地方。

您链接的文档 N4620 是 Ranges 的工作草案 ,而不是 Concepts。除非你包含一个 -fconcepts 友好的范围实现,否则你不会得到为你定义的 RandomAccessIterator

因为 Tristan Brindle mentioned in , cmcstl2 是 Ranges TS 的参考实现。

Concepts TS 实际上并不包含任何标准概念,例如 RandomAccessIterator。这些正在单独的 Ranges TS.

中进行处理

Ranges TS 的参考实现可在 https://github.com/CaseyCarter/cmcstl2 获得。