#include <iostream>
#include <utility>
template <typename T>
class Condition {
public:
using ItemType = T;
};
class OneCondition : public Condition<int>
{
public:
bool match(const Condition::ItemType& item) const
{
return item == 1;
}
};
class TwoCondition : public Condition<int>
{
public:
bool match(const Condition::ItemType& item) const
{
return item == 2;
}
};
template <typename T>
concept bool IsCondition = requires(T a, typename T::ItemType b) {
{ a.match(b) } -> bool;
};
template <typename U, typename V>
class ConditionAnd {
public:
ConditionAnd(U&& first, V&& second) :
first_(std::forward<U>(first)), second_(std::forward<V>(second)) {
}
template <typename W>
bool match(const W& item) const {
return first_.match(item) && second_.match(item);
}
private:
U first_;
V second_;
};
template <IsCondition U, IsCondition V>
decltype(auto) operator&&(U&& first, V&& second)
{
return ConditionAnd(std::forward<U>(first), std::forward<V>(second));
}
int main() {
auto cond = OneCondition{} && TwoCondition{};
std::cout << cond.match(4) << std::endl;
std::cout << "Hello, world!" << std::endl;
}
#include <iostream>
#include <utility>
class OneCondition
{
public:
bool match(const int& item) const
{
return item == 1;
}
};
class TwoCondition
{
public:
bool match(const int& item) const
{
return item == 2;
}
};
template <typename T, typename U>
concept bool Condition = requires(T a, U b) {
{ a.match(b) } -> bool;
};
template <Condition T, Condition U>
class ConditionAnd {
public:
ConditionAnd(T&& first, U&& second) :
first_(std::forward<T>(first)), second_(std::forward<U>(second)) {
}
template <typename V>
bool match(const V& item) const {
return first_.match(item) && second_.match(item);
}
private:
T first_;
U second_;
};
template <Condition T, Condition U>
decltype(auto) operator&&(T&& first, U&& second) {
return ConditionAnd(std::forward<T>(first), std::forward<U>(second));
}
int main() {
auto cond = OneCondition{} && TwoCondition{};
std::cout << cond.match(4) << std::endl;
std::cout << "Hello, world!" << std::endl;
}
/home/maxim/Workspace/my/cdm/main.cpp:31:11: error: ‘Condition’ is not a type
template <Condition T, Condition U>
^~~~~~~~~
/home/maxim/Workspace/my/cdm/main.cpp:31:24: error: ‘Condition’ is not a type
template <Condition T, Condition U>
^~~~~~~~~
/home/maxim/Workspace/my/cdm/main.cpp:34:19: error: expected ‘)’ before ‘&&’ token
ConditionAnd(T&& first, U&& second) :
~ ^~
)
/home/maxim/Workspace/my/cdm/main.cpp:44:5: error: ‘T’ does not name a type
T first_;
^
/home/maxim/Workspace/my/cdm/main.cpp:45:5: error: ‘U’ does not name a type
U second_;
^
/home/maxim/Workspace/my/cdm/main.cpp:48:11: error: ‘Condition’ is not a type
template <Condition T, Condition U>
^~~~~~~~~
/home/maxim/Workspace/my/cdm/main.cpp:48:24: error: ‘Condition’ is not a type
template <Condition T, Condition U>
^~~~~~~~~
/home/maxim/Workspace/my/cdm/main.cpp:49:27: error: declaration of ‘operator&&’ as non-function
decltype(auto) operator&&(T&& first, U&& second) {
^
/home/maxim/Workspace/my/cdm/main.cpp:49:27: error: ‘T’ was not declared in this scope
/home/maxim/Workspace/my/cdm/main.cpp:49:31: error: ‘first’ was not declared in this scope
decltype(auto) operator&&(T&& first, U&& second) {
^~~~~
/home/maxim/Workspace/my/cdm/main.cpp:49:31: note: suggested alternative: ‘fpos_t’
decltype(auto) operator&&(T&& first, U&& second) {
^~~~~
fpos_t
/home/maxim/Workspace/my/cdm/main.cpp:49:38: error: ‘U’ was not declared in this scope
decltype(auto) operator&&(T&& first, U&& second) {
^
/home/maxim/Workspace/my/cdm/main.cpp:49:42: error: ‘second’ was not declared in this scope
decltype(auto) operator&&(T&& first, U&& second) {
^~~~~~
/home/maxim/Workspace/my/cdm/main.cpp:49:42: note: suggested alternative: ‘rewind’
decltype(auto) operator&&(T&& first, U&& second) {
^~~~~~
rewind
/home/maxim/Workspace/my/cdm/main.cpp: In function ‘int main()’:
/home/maxim/Workspace/my/cdm/main.cpp:54:32: error: no match for ‘operator&&’ (operand types are ‘OneCondition’ and ‘TwoCondition’)
auto cond = OneCondition{} && TwoCondition{};
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/home/maxim/Workspace/my/cdm/main.cpp:54:32: note: candidate: ‘operator&&(bool, bool)’ <built-in>
/home/maxim/Workspace/my/cdm/main.cpp:54:32: note: no known conversion for argument 2 from ‘TwoCondition’ to ‘bool’
make[2]: *** [CMakeFiles/cdm.dir/build.make:63: CMakeFiles/cdm.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/cdm.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
template <typename T>
concept bool Any = true;
template <typename T>
concept bool Condition = requires(T a, Any b) {
{ a.match(b) } -> bool;
};
// Or
template <typename T>
concept bool Condition = requires(T a, auto b) {
{ a.match(b) } -> bool;
};
/home/maxim/Workspace/my/cdm/main.cpp:27:45: internal compiler error: in synthesize_implicit_template_parm, at cp/parser.c:39141
concept bool Condition = requires(T a, auto b) {
^
0x7ff07d87c09a __libc_start_main
../csu/libc-start.c:308
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <file:///usr/share/doc/gcc-8/README.Bugs> for instructions.
make[2]: *** [CMakeFiles/cdm.dir/build.make:63: CMakeFiles/cdm.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/cdm.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
~ >>> g++ --version
g++ (Debian 8.3.0-6) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.