Skip to content

Instantly share code, notes, and snippets.

@congdanhqx-zz
Created July 25, 2016 18:28
Show Gist options
  • Save congdanhqx-zz/65e113a390ead8d168a59e7e43752445 to your computer and use it in GitHub Desktop.
Save congdanhqx-zz/65e113a390ead8d168a59e7e43752445 to your computer and use it in GitHub Desktop.
C++ Concepts simpliest example
#include <list>
#include <iostream>
#include <algorithm>
#if defined(__cpp_concepts) && __cpp_concepts >= 201500
template<typename T>
concept bool HasMemberNamedTestConcept = requires(T t) {
t.test;
};
template<HasMemberNamedTestConcept T>
auto callConceptAsTemplateParameter(T t) {
return t.test, t.test;
}
#endif
template<typename T>
#if defined(__cpp_concepts) && __cpp_concepts >= 201500
requires HasMemberNamedTestConcept<T>
#endif
auto orUseRequireAfterTemplateParamList(T t) {
return t.test;
}
template<typename T>
auto orUseRequireAfterDeclaration(T t)
#if defined(__cpp_concepts) && __cpp_concepts >= 201500
requires HasMemberNamedTestConcept<T>
#endif
{
return t.test;
}
struct NoTest {
};
struct HaveTest {
int test;
};
int main()
{
#if defined(__cpp_concepts) && __cpp_concepts >= 201500
std::cout << __cpp_concepts;
auto x1 = callConceptAsTemplateParameter(NoTest{});
auto y1 = callConceptAsTemplateParameter(HaveTest{});
#endif
auto x2 = orUseRequireAfterTemplateParamList(NoTest{});
auto y2 = orUseRequireAfterTemplateParamList(HaveTest{});
auto x3 = orUseRequireAfterDeclaration(NoTest{});
auto y3 = orUseRequireAfterDeclaration(HaveTest{});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment