├── .appveyor.yml ├── .clang-format ├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── LICENSE ├── Makefile ├── README.md ├── cmake └── dummy-config.cmake.in ├── doc └── reference.md ├── include └── tao │ └── operators.hpp └── src └── test └── operators ├── CMakeLists.txt ├── no_rvalue_reference_results.cpp └── test_operators.cpp /.appveyor.yml: -------------------------------------------------------------------------------- 1 | version: '{branch}-{build}' 2 | 3 | os: Visual Studio 2015 4 | 5 | platform: 6 | - x86 7 | - x64 8 | 9 | configuration: 10 | - Release 11 | - Debug 12 | 13 | environment: 14 | matrix: 15 | - GENERATOR: Visual Studio 14 2015 16 | 17 | - GENERATOR: Visual Studio 14 2015 Win64 18 | 19 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 20 | GENERATOR: Visual Studio 15 2017 21 | 22 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 23 | GENERATOR: Visual Studio 15 2017 Win64 24 | 25 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 26 | GENERATOR: Visual Studio 16 2019 27 | 28 | - GENERATOR: MinGW Makefiles 29 | BINDIR: C:\MinGW\bin 30 | 31 | - GENERATOR: MinGW Makefiles 32 | COMPILER: MinGW 5 33 | BINDIR: C:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin 34 | 35 | - GENERATOR: MinGW Makefiles 36 | COMPILER: MinGW 6 37 | BINDIR: C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev1\mingw32\bin 38 | 39 | - GENERATOR: MinGW Makefiles 40 | COMPILER: MinGW 6 41 | BINDIR: C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin 42 | 43 | init: [] 44 | 45 | before_build: 46 | - if "%APPVEYOR_BUILD_WORKER_IMAGE%"=="Visual Studio 2015" (del "C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets") 47 | - if defined BINDIR (set "PATH=%BINDIR%;%PATH:C:\Program Files\Git\usr\bin;=%") 48 | - md build 49 | - cd build 50 | - cmake -Wno-dev -DPEGTL_BUILD_EXAMPLES=OFF --config "%CONFIGURATION%" -G "%GENERATOR%" .. 51 | 52 | build_script: 53 | - cmake --build . --config "%CONFIGURATION%" 54 | 55 | test_script: 56 | - ctest -C "%CONFIGURATION%" --output-on-failure 57 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | # the official .clang-format style for https://github.com/taocpp 2 | # 3 | # clang-format -i -style=file $(find . -name '[^.]*.[hc]pp') 4 | 5 | Language: Cpp 6 | Standard: Cpp11 7 | 8 | AccessModifierOffset: -3 9 | AlignAfterOpenBracket: Align 10 | AlignConsecutiveAssignments: false 11 | AlignConsecutiveDeclarations: false 12 | AlignEscapedNewlinesLeft: false 13 | AlignOperands: true 14 | AlignTrailingComments: true 15 | AllowAllParametersOfDeclarationOnNextLine: true 16 | AllowShortBlocksOnASingleLine: false 17 | AllowShortCaseLabelsOnASingleLine: false 18 | AllowShortFunctionsOnASingleLine: Empty 19 | AllowShortIfStatementsOnASingleLine: false 20 | AllowShortLoopsOnASingleLine: false 21 | AlwaysBreakAfterReturnType: None 22 | AlwaysBreakBeforeMultilineStrings: false 23 | AlwaysBreakTemplateDeclarations: Yes 24 | BinPackArguments: false 25 | BinPackParameters: false 26 | BraceWrapping: 27 | AfterClass: true 28 | AfterControlStatement: false 29 | AfterEnum : true 30 | AfterFunction : true 31 | AfterNamespace : true 32 | AfterStruct : true 33 | AfterUnion : true 34 | AfterExternBlock: true 35 | BeforeCatch : true 36 | BeforeElse : true 37 | IndentBraces : false 38 | SplitEmptyFunction: false 39 | SplitEmptyRecord: false 40 | SplitEmptyNamespace: false 41 | BreakBeforeBinaryOperators: All 42 | BreakBeforeBraces: Custom 43 | BreakBeforeTernaryOperators: false 44 | BreakConstructorInitializers: BeforeColon 45 | BreakInheritanceList: BeforeColon 46 | BreakStringLiterals: false 47 | ColumnLimit: 0 48 | CompactNamespaces: false 49 | ConstructorInitializerAllOnOneLineOrOnePerLine: true 50 | ConstructorInitializerIndentWidth: 3 51 | ContinuationIndentWidth: 3 52 | Cpp11BracedListStyle: false 53 | DerivePointerAlignment: false 54 | DisableFormat: false 55 | ExperimentalAutoDetectBinPacking: false 56 | FixNamespaceComments: true 57 | IncludeBlocks: Preserve 58 | IndentCaseLabels: true 59 | IndentPPDirectives: None 60 | IndentWidth: 3 61 | IndentWrappedFunctionNames: false 62 | KeepEmptyLinesAtTheStartOfBlocks: false 63 | MaxEmptyLinesToKeep: 1 64 | NamespaceIndentation: All 65 | PointerAlignment: Left 66 | ReflowComments: false 67 | SortIncludes: true 68 | SortUsingDeclarations: false 69 | SpaceAfterCStyleCast: false 70 | SpaceAfterTemplateKeyword: false 71 | SpaceBeforeAssignmentOperators: true 72 | SpaceBeforeCpp11BracedList: false 73 | SpaceBeforeCtorInitializerColon: true 74 | SpaceBeforeInheritanceColon: true 75 | SpaceBeforeParens: Never 76 | SpaceBeforeRangeBasedForLoopColon: true 77 | SpaceInEmptyParentheses: false 78 | SpacesBeforeTrailingComments: 2 79 | SpacesInAngles: true 80 | SpacesInCStyleCastParentheses: false 81 | SpacesInParentheses: true 82 | SpacesInSquareBrackets: true 83 | TabWidth: 8 84 | UseTab: Never 85 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | build 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: generic 2 | os: linux 3 | dist: xenial 4 | 5 | matrix: 6 | include: 7 | - dist: precise 8 | compiler: gcc 9 | addons: 10 | apt: 11 | sources: ['ubuntu-toolchain-r-test'] 12 | packages: ['g++-4.7'] 13 | env: CXX=g++-4.7 14 | 15 | - compiler: gcc 16 | addons: 17 | apt: 18 | packages: ['g++-4.8'] 19 | env: 20 | - CXX=g++-4.8 21 | 22 | - compiler: gcc 23 | addons: 24 | apt: 25 | packages: ['g++-4.9'] 26 | env: 27 | - CXX=g++-4.9 28 | 29 | - compiler: gcc 30 | addons: 31 | apt: 32 | packages: ['g++-5'] 33 | env: 34 | - CXX=g++-5 35 | 36 | - compiler: gcc 37 | addons: 38 | apt: 39 | packages: ['g++-5'] 40 | env: 41 | - CXX=g++-5 42 | - CXXSTD=-std=c++14 43 | 44 | - compiler: gcc 45 | addons: 46 | apt: 47 | sources: ['ubuntu-toolchain-r-test'] 48 | packages: ['g++-6'] 49 | env: 50 | - CXX=g++-6 51 | 52 | - compiler: gcc 53 | addons: 54 | apt: 55 | sources: ['ubuntu-toolchain-r-test'] 56 | packages: ['g++-7'] 57 | env: 58 | - CXX=g++-7 59 | 60 | - compiler: gcc 61 | addons: 62 | apt: 63 | sources: ['ubuntu-toolchain-r-test'] 64 | packages: ['g++-7'] 65 | env: 66 | - CXX=g++-7 67 | - CXXSTD=-std=c++17 68 | 69 | - compiler: gcc 70 | addons: 71 | apt: 72 | sources: ['ubuntu-toolchain-r-test'] 73 | packages: ['g++-8'] 74 | env: 75 | - CXX=g++-8 76 | 77 | - compiler: gcc 78 | addons: 79 | apt: 80 | sources: ['ubuntu-toolchain-r-test'] 81 | packages: ['g++-9'] 82 | env: 83 | - CXX=g++-9 84 | 85 | - dist: precise 86 | compiler: clang 87 | env: 88 | - CXX=clang++ 89 | 90 | - compiler: clang 91 | addons: 92 | apt: 93 | packages: ['clang-3.5'] 94 | env: 95 | - CXX=clang++-3.5 96 | 97 | - compiler: clang 98 | addons: 99 | apt: 100 | packages: ['clang-3.6'] 101 | env: 102 | - CXX=clang++-3.6 103 | 104 | - compiler: clang 105 | addons: 106 | apt: 107 | packages: ['clang-3.7'] 108 | env: 109 | - CXX=clang++-3.7 110 | 111 | - compiler: clang 112 | addons: 113 | apt: 114 | packages: ['clang-3.8'] 115 | env: 116 | - CXX=clang++-3.8 117 | 118 | - compiler: clang 119 | addons: 120 | apt: 121 | packages: ['clang-3.9'] 122 | env: 123 | - CXX=clang++-3.9 124 | 125 | - compiler: clang 126 | addons: 127 | apt: 128 | sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-xenial-4.0'] 129 | packages: ['clang-4.0'] 130 | env: 131 | - CXX=clang++-4.0 132 | 133 | - compiler: clang 134 | addons: 135 | apt: 136 | sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-xenial-5.0'] 137 | packages: ['clang-5.0'] 138 | env: 139 | - CXX=clang++-5.0 140 | 141 | - compiler: clang 142 | addons: 143 | apt: 144 | sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-xenial-6.0'] 145 | packages: ['clang-6.0'] 146 | env: 147 | - CXX=clang++-6.0 148 | 149 | - compiler: clang 150 | addons: 151 | apt: 152 | sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-xenial-7'] 153 | packages: ['clang-7'] 154 | env: 155 | - CXX=clang++-7 156 | 157 | - compiler: clang 158 | addons: 159 | apt: 160 | sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-xenial-8'] 161 | packages: ['clang-8'] 162 | env: 163 | - CXX=clang++-8 164 | 165 | - compiler: clang 166 | addons: 167 | apt: 168 | sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-xenial-8'] 169 | packages: ['clang-8'] 170 | env: 171 | - CXX=clang++-8 172 | - CPPFLAGS=-fms-extensions 173 | 174 | - os: osx 175 | osx_image: xcode6.4 176 | compiler: clang 177 | env: 178 | - CXX=clang++ 179 | 180 | - os: osx 181 | osx_image: xcode7.3 182 | compiler: clang 183 | env: 184 | - CXX=clang++ 185 | 186 | - os: osx 187 | osx_image: xcode8.3 188 | compiler: clang 189 | env: 190 | - CXX=clang++ 191 | 192 | - os: osx 193 | osx_image: xcode9.4 194 | compiler: clang 195 | env: 196 | - CXX=clang++ 197 | 198 | - os: osx 199 | osx_image: xcode10.2 200 | compiler: clang 201 | env: 202 | - CXX=clang++ 203 | 204 | - compiler: gcc 205 | addons: 206 | apt: 207 | sources: ['ubuntu-toolchain-r-test'] 208 | packages: ['g++-7'] 209 | env: 210 | - CXX=g++-7 211 | - CXXFLAGS="-O0 --coverage" 212 | before_script: 213 | - pip install --user cpp-coveralls 214 | after_success: 215 | - coveralls --gcov gcov-7 --gcov-options '\-lp' --exclude src 216 | 217 | - compiler: clang 218 | addons: 219 | apt: 220 | sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-xenial-8'] 221 | packages: ['clang-8', 'clang-tools-8'] 222 | env: 223 | - CXX=clang++-8 224 | script: 225 | - scan-build-8 make -kj3 226 | 227 | - compiler: clang 228 | addons: 229 | apt: 230 | sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-xenial-8'] 231 | packages: ['clang-8', 'clang-tidy-8'] 232 | env: 233 | - CXX=clang++-8 234 | - CLANG_TIDY=clang-tidy-8 235 | script: 236 | - make -kj3 clang-tidy 237 | 238 | - compiler: gcc 239 | addons: 240 | apt: 241 | sources: ['ubuntu-toolchain-r-test'] 242 | packages: ['g++-9'] 243 | env: 244 | - CXX=g++-9 245 | script: 246 | - $CXX --version 247 | - mkdir build 248 | - cd build 249 | - cmake .. 250 | - cmake --build . 251 | - cmake --build . --target test 252 | 253 | script: 254 | - $CXX --version 255 | - make -kj3 256 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR) 2 | 3 | project(taocpp-operators VERSION 1.2.2 LANGUAGES CXX) 4 | 5 | if(${PROJECT_NAME}_FOUND) 6 | # multiple versions can't co-exist 7 | if(NOT ${PROJECT_NAME}_VERSION STREQUAL ${PROJECT_VERSION}) 8 | message(FATAL_ERROR "Multiple mismatched versions") 9 | endif() 10 | 11 | # only include if this is the first include 12 | if(NOT ${PROJECT_NAME}_DIR STREQUAL "${PROJECT_BINARY_DIR}") 13 | return() 14 | endif() 15 | endif() 16 | 17 | # keep track of version 18 | set(${PROJECT_NAME}_FOUND TRUE CACHE BOOL "" FORCE) 19 | set(${PROJECT_NAME}_VERSION "${PROJECT_VERSION}" CACHE STRING "" FORCE) 20 | set(${PROJECT_NAME}_DIR "${PROJECT_BINARY_DIR}" CACHE PATH "" FORCE) 21 | 22 | mark_as_advanced(${PROJECT_NAME}_FOUND) 23 | mark_as_advanced(${PROJECT_NAME}_VERSION) 24 | mark_as_advanced(${PROJECT_NAME}_DIR) 25 | 26 | # installation directories 27 | set(TAOCPP_OPERATORS_INSTALL_INCLUDE_DIR "include" CACHE STRING "The installation include directory") 28 | set(TAOCPP_OPERATORS_INSTALL_DOC_DIR "share/doc/tao/operators" CACHE STRING "The installation doc directory") 29 | set(TAOCPP_OPERATORS_INSTALL_CMAKE_DIR "share/taocpp-operators/cmake" CACHE STRING "The installation cmake directory") 30 | 31 | # define a header-only library 32 | add_library(operators INTERFACE) 33 | add_library(taocpp::operators ALIAS operators) 34 | target_include_directories(operators INTERFACE 35 | $ 36 | $ 37 | ) 38 | 39 | # features used by taocpp/operators 40 | target_compile_features(operators INTERFACE 41 | cxx_constexpr 42 | cxx_noexcept 43 | cxx_rvalue_references 44 | ) 45 | 46 | # testing 47 | enable_testing() 48 | option(TAOCPP_OPERATORS_BUILD_TESTS "Build test programs" ON) 49 | if(TAOCPP_OPERATORS_BUILD_TESTS) 50 | add_subdirectory(src/test/operators) 51 | endif() 52 | 53 | # make package findable 54 | configure_file(cmake/dummy-config.cmake.in taocpp-operators-config.cmake @ONLY) 55 | 56 | # install and export target 57 | install(TARGETS operators EXPORT operators-targets) 58 | 59 | install(EXPORT operators-targets 60 | FILE taocpp-operators-config.cmake 61 | NAMESPACE taocpp:: 62 | DESTINATION ${TAOCPP_OPERATORS_INSTALL_CMAKE_DIR} 63 | ) 64 | 65 | install(DIRECTORY include/ DESTINATION ${TAOCPP_OPERATORS_INSTALL_INCLUDE_DIR}) 66 | install(FILES LICENSE DESTINATION ${TAOCPP_OPERATORS_INSTALL_DOC_DIR}) 67 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013-2020 Daniel Frey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # The Art of C++ 2 | # Copyright (c) 2015-2020 Daniel Frey 3 | # Please see LICENSE for license or visit https://github.com/taocpp/json 4 | 5 | .SUFFIXES: 6 | .SECONDARY: 7 | 8 | ifeq ($(OS),Windows_NT) 9 | UNAME_S := $(OS) 10 | else 11 | UNAME_S := $(shell uname -s) 12 | endif 13 | 14 | # For Darwin (Mac OS X) we assume that the default compiler 15 | # clang++ is used; when $(CXX) is some version of g++, then 16 | # $(CXXSTD) has to be set to -std=c++11 (or newer) so 17 | # that -stdlib=libc++ is not automatically added. 18 | 19 | ifeq ($(CXXSTD),) 20 | CXXSTD := -std=c++11 21 | ifeq ($(UNAME_S),Darwin) 22 | CXXSTD += -stdlib=libc++ 23 | endif 24 | endif 25 | 26 | # Ensure strict standard compliance and no warnings, can be 27 | # changed if desired. 28 | 29 | CPPFLAGS ?= -pedantic 30 | CXXFLAGS ?= -Wall -Wextra -Wshadow -Werror -O3 31 | 32 | CLANG_TIDY ?= clang-tidy 33 | 34 | HEADERS := $(shell find include -name '*.hpp') 35 | SOURCES := $(shell find src -name '*.cpp') 36 | DEPENDS := $(SOURCES:%.cpp=build/%.d) 37 | BINARIES := $(SOURCES:%.cpp=build/%) 38 | 39 | UNIT_TESTS := $(filter build/src/test/%,$(BINARIES)) 40 | 41 | .PHONY: all 42 | all: compile check 43 | 44 | .PHONY: compile 45 | compile: $(BINARIES) 46 | 47 | .PHONY: check 48 | check: $(UNIT_TESTS) 49 | @set -e; for T in $(UNIT_TESTS); do echo $$T; $$T > /dev/null; done 50 | 51 | .PHONY: clean 52 | clean: 53 | @rm -rf build 54 | @find . -name '*~' -delete 55 | 56 | build/%.d: %.cpp Makefile 57 | @mkdir -p $(@D) 58 | $(CXX) $(CXXSTD) -Iinclude $(CPPFLAGS) -MM -MQ $@ $< -o $@ 59 | 60 | build/%: %.cpp build/%.d 61 | $(CXX) $(CXXSTD) -Iinclude $(CPPFLAGS) $(CXXFLAGS) $< -o $@ 62 | 63 | build/%.clang-tidy: % 64 | $(CLANG_TIDY) -extra-arg "-Iinclude" -extra-arg "-std=c++11" -checks=*,-fuchsia-*,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-macro-usage,-misc-macro-parentheses,-hicpp-no-array-decay -warnings-as-errors=* $< 2>/dev/null 65 | @mkdir -p $(@D) 66 | @touch $@ 67 | 68 | .PHONY: clang-tidy 69 | clang-tidy: $(HEADERS:%=build/%.clang-tidy) $(SOURCES:%=build/%.clang-tidy) 70 | @echo "All $(words $(HEADERS) $(SOURCES)) clang-tidy tests passed." 71 | 72 | ifeq ($(findstring $(MAKECMDGOALS),clean),) 73 | -include $(DEPENDS) 74 | endif 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Art of C++ / Operators 2 | 3 | [![Release](https://img.shields.io/github/release/taocpp/operators.svg)](https://github.com/taocpp/operators/releases/latest) 4 | [![Download](https://api.bintray.com/packages/conan/conan-center/taocpp-operatorss%3A_/images/download.svg)](https://bintray.com/conan/conan-center/taocpp-operators%3A_/_latestVersion) 5 | [![TravisCI](https://travis-ci.org/taocpp/operators.svg?branch=main)](https://travis-ci.org/taocpp/operators) 6 | [![AppVeyor](https://ci.appveyor.com/api/projects/status/794d875ucgic4sq0/branch/main?svg=true)](https://ci.appveyor.com/project/taocpp/operators) 7 | [![Coverage](https://coveralls.io/repos/github/taocpp/operators/badge.svg?branch=main)](https://coveralls.io/github/taocpp/operators) 8 | [![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/taocpp/operators.svg)](https://lgtm.com/projects/g/taocpp/operators/context:cpp) 9 | 10 | [The Art of C++](https://taocpp.github.io/) / Operators is a zero-dependency C++11 single-header library that provides highly efficient, move aware operators for arithmetic data types. 11 | 12 | ### Table of Content 13 | 14 | [Overview](#overview)
15 | [Example](#example)
16 | [Requirements](#requirements)
17 | [Installation](#installation)
18 | [Provided Templates](#provided-templates)
19 | [Commutativity](#commutativity)
20 | [RValue References](#rvalue-references)
21 | [constexpr](#constexpr)
22 | [noexcept](#noexcept)
23 | [nodiscard](#nodiscard)
24 | [Changelog](#changelog)
25 | [History](#history)
26 | [License](#license) 27 | 28 | ## Overview 29 | 30 | Overloaded operators for class types typically don't come alone. 31 | For example, when `x + y` is possible then `x += y` should be, too. 32 | When `x < y` is possible then `x > y`, `x >= y`, and `x <= y` should be, too. 33 | 34 | Implementing large sets of operators, possibly for multiple classes, is both tedious and error-prone. 35 | However, more often than not, some of these operators can be defined in terms of others. 36 | For example `x >= y` can frequently be defined as `!(x < y)`. 37 | 38 | Given the implementation of some basic operators, the templates in the Art of C++ / Operators can generate many more operators automatically. 39 | 40 | The generated operators are overloaded to take advantage of movable types, and allow the compiler to avoid unneccessary temporary objects wherever possible. 41 | All generated operators are `noexcept` when the underlying operations are `noexcept`. 42 | Generated comparison operators are `constexpr` (when supported by the compiler). 43 | 44 | ## Example 45 | 46 | Given this dummy integer class... 47 | 48 | ```c++ 49 | #include 50 | 51 | class MyInt 52 | : tao::operators::commutative_addable< MyInt >, 53 | tao::operators::multipliable< MyInt, double > 54 | { 55 | public: 56 | // create a new instance of MyInt 57 | MyInt( const int v ) noexcept; 58 | 59 | // copy and move constructor 60 | MyInt( const MyInt& v ) noexcept; 61 | MyInt( MyInt&& v ) noexcept; // optional 62 | 63 | // copy and move assignment 64 | MyInt& operator=( const MyInt& v ) noexcept; 65 | MyInt& operator=( MyInt&& v ) noexcept; // optional 66 | 67 | // addition of another MyInt 68 | MyInt& operator+=( const MyInt& v ) noexcept; 69 | MyInt& operator+=( MyInt&& v ) noexcept; // optional 70 | 71 | // multiplication by a scalar 72 | MyInt& operator*=( const double v ) noexcept; 73 | }; 74 | ``` 75 | 76 | ...the base class templates will *generate* the following operators. 77 | 78 | ```c++ 79 | // generated by tao::operators::commutative_addable< MyInt > 80 | MyInt operator+( const MyInt& lhs, const MyInt& rhs ) noexcept; 81 | MyInt&& operator+( const MyInt& lhs, MyInt&& rhs ) noexcept; 82 | MyInt&& operator+( MyInt&& lhs, const MyInt& rhs ) noexcept; 83 | MyInt&& operator+( MyInt&& lhs, MyInt&& rhs ) noexcept; 84 | 85 | // generated by tao::operators::multipliable< MyInt, double > 86 | MyInt operator*( const MyInt& lhs, const double& rhs ) noexcept; 87 | MyInt operator*( const MyInt& lhs, double&& rhs ) noexcept; 88 | MyInt&& operator*( MyInt&& lhs, const double& rhs ) noexcept; 89 | MyInt&& operator*( MyInt&& lhs, double&& rhs ) noexcept; 90 | ``` 91 | 92 | >Note: The `// optional` in `class MyInt` above marks methods 93 | >that you typically only add when your class benefits from an 94 | >rvalue reference parameter. If there is no benefit for the 95 | >implementation, you can just omit these methods. If you leave 96 | >them out, The Art of C++ / Operators will simply call the corresponding 97 | >non-movable version that takes the parameter by const lvalue 98 | >reference. 99 | 100 | ## Requirements 101 | 102 | Requires C++11 or newer. Tested with: 103 | 104 | * GCC 4.7+ 105 | * Clang 3.2+ 106 | * Visual Studio 2015+ 107 | 108 | Remember to enable C++11, e.g., provide `-std=c++11` or similar options. 109 | 110 | >Note: If you use or test the library with other compilers/versions, 111 | >e.g., Visual C++, Intel C++, or any other compiler we'd like to hear from you. 112 | 113 | >Note: For compilers that don't support `noexcept`, see chapter [noexcept](#noexcept). 114 | 115 | ## Installation 116 | 117 | The Art of C++ / Operators is a single-header library. There is nothing to build or install, 118 | just copy the header somewhere and include it in your code. 119 | 120 | ## Package Managers 121 | 122 | You can download and install taocpp-operators using the [Conan](https://github.com/conan-io/conan) package manager: 123 | 124 | conan install taocpp-operators/1.2.2@ 125 | 126 | The taocpp-operators package in conan is kept up to date by Conan team members and community contributors. 127 | If the version is out-of-date, please [create an issue or pull request](https://github.com/conan-io/conan-center-index) on the Conan Center Index repository. 128 | 129 | ## Provided Templates 130 | 131 | The following table gives an overview of the available templates. 132 | Note that the "Provides" and "Requires" columns are just a basic overview. 133 | Multiple overloads per provided operator might exist to ensure the most 134 | efficient implementation for each case, exploiting move-semantics when 135 | possible and (unless explicitly disabled) pass-through of temporary 136 | values to avoid creating new temporaries. 137 | 138 | Each overload of an operator is marked `noexcept` when the required operation(s) 139 | that are used to implement it are also marked `noexcept`. 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 156 | 157 | 166 | 167 | 168 | 169 | 170 | 179 | 180 | 193 | 194 | 195 | 196 | 197 | 204 | 205 | 213 | 214 | 215 | 216 | 217 | 227 | 228 | 242 | 243 | 244 | 245 | 246 | 253 | 254 | 262 | 263 | 270 | 271 | 278 | 279 | 287 | 288 | 289 | 290 | 291 | 298 | 299 | 306 | 307 | 315 | 316 | 317 | 318 | 319 | 326 | 327 | 335 | 336 | 343 | 344 | 351 | 352 | 360 | 361 | 362 | 363 | 364 | 371 | 372 | 379 | 380 | 388 | 389 | 390 | 391 | 392 | 399 | 400 | 407 | 408 | 416 | 417 | 418 | 419 | 420 | 427 | 428 | 436 | 437 | 444 | 445 | 452 | 453 | 461 | 462 | 463 | 464 | 465 | 472 | 473 | 481 | 482 | 489 | 490 | 497 | 498 | 506 | 507 | 508 | 509 | 510 | 517 | 518 | 526 | 527 | 534 | 535 | 542 | 543 | 551 | 552 | 553 | 554 | 555 | 562 | 563 | 570 | 571 | 572 | 573 | 574 | 581 | 582 | 589 | 590 | 591 | 592 | 593 | 600 | 601 | 602 | 603 | 604 | 611 | 612 | 613 |
TemplateProvidesRequires
150 | equality_comparable< T > 151 | 152 | T != T 153 | 154 | T == T 155 |
158 | equality_comparable< T, U > 159 | 160 | T != U
161 | U == T
162 | U != T 163 |
164 | T == U 165 |
171 | less_than_comparable< T > 172 | 173 | T > T
174 | T <= T
175 | T >= T 176 |
177 | T < T 178 |
181 | less_than_comparable< T, U > 182 | 183 | T <= U
184 | T >= U
185 | U < T
186 | U > T
187 | U <= T
188 | U >= T 189 |
190 | T < U
191 | T > U 192 |
198 | equivalent< T > 199 | 200 | T == T 201 | 202 | T < T 203 |
206 | equivalent< T, U > 207 | 208 | T == U 209 | 210 | T < U
211 | T > U 212 |
218 | partially_ordered< T > 219 | 220 | T > T
221 | T <= T
222 | T >= T 223 |
224 | T < T
225 | T == T 226 |
229 | partially_ordered< T, U > 230 | 231 | T <= U
232 | T >= U
233 | U < T
234 | U > T
235 | U <= T
236 | U >= T 237 |
238 | T < U
239 | T > U
240 | T == U 241 |
247 | commutative_addable< T > 248 | 249 | T + T 250 | 251 | T += T 252 |
255 | commutative_addable< T, U > 256 | 257 | T + U
258 | U + T 259 |
260 | T += U 261 |
264 | addable< T > 265 | 266 | T + T 267 | 268 | T += T 269 |
272 | addable< T, U > 273 | 274 | T + U 275 | 276 | T += U 277 |
280 | addable_left< T, U > 281 | 282 | U + T 283 | 284 | T( U )
285 | T += T 286 |
292 | subtractable< T > 293 | 294 | T - T 295 | 296 | T -= T 297 |
300 | subtractable< T, U > 301 | 302 | T - U 303 | 304 | T -= U 305 |
308 | subtractable_left< T, U > 309 | 310 | U - T 311 | 312 | T( U )
313 | T -= T 314 |
320 | commutative_multipliable< T > 321 | 322 | T * T 323 | 324 | T *= T 325 |
328 | commutative_multipliable< T, U > 329 | 330 | T * U
331 | U * T 332 |
333 | T *= U 334 |
337 | multipliable< T > 338 | 339 | T * T 340 | 341 | T *= T 342 |
345 | multipliable< T, U > 346 | 347 | T * U 348 | 349 | T *= U 350 |
353 | multipliable_left< T, U > 354 | 355 | U * T 356 | 357 | T( U )
358 | T *= T 359 |
365 | dividable< T > 366 | 367 | T / T 368 | 369 | T /= T 370 |
373 | dividable< T, U > 374 | 375 | T / U 376 | 377 | T /= U 378 |
381 | dividable_left< T, U > 382 | 383 | U / T 384 | 385 | T( U )
386 | T /= T 387 |
393 | modable< T > 394 | 395 | T % T 396 | 397 | T %= T 398 |
401 | modable< T, U > 402 | 403 | T % U 404 | 405 | T %= U 406 |
409 | modable_left< T, U > 410 | 411 | U % T 412 | 413 | T( U )
414 | T %= T 415 |
421 | commutative_andable< T > 422 | 423 | T & T 424 | 425 | T &= T 426 |
429 | commutative_andable< T, U > 430 | 431 | T & U
432 | U & T 433 |
434 | T &= U 435 |
438 | andable< T > 439 | 440 | T & T 441 | 442 | T &= T 443 |
446 | andable< T, U > 447 | 448 | T & U 449 | 450 | T &= U 451 |
454 | andable_left< T, U > 455 | 456 | U & T 457 | 458 | T( U )
459 | T &= T 460 |
466 | commutative_orable< T > 467 | 468 | T | T 469 | 470 | T |= T 471 |
474 | commutative_orable< T, U > 475 | 476 | T | U
477 | U | T 478 |
479 | T |= U 480 |
483 | orable< T > 484 | 485 | T | T 486 | 487 | T |= T 488 |
491 | orable< T, U > 492 | 493 | T | U 494 | 495 | T |= U 496 |
499 | orable_left< T, U > 500 | 501 | U | T 502 | 503 | T( U )
504 | T |= T 505 |
511 | commutative_xorable< T > 512 | 513 | T ^ T 514 | 515 | T ^= T 516 |
519 | commutative_xorable< T, U > 520 | 521 | T ^ U
522 | U ^ T 523 |
524 | T ^= U 525 |
528 | xorable< T > 529 | 530 | T ^ T 531 | 532 | T ^= T 533 |
536 | xorable< T, U > 537 | 538 | T ^ U 539 | 540 | T ^= U 541 |
544 | xorable_left< T, U > 545 | 546 | U ^ T 547 | 548 | T( U )
549 | T ^= T 550 |
556 | left_shiftable< T > 557 | 558 | T << T 559 | 560 | T <<= T 561 |
564 | left_shiftable< T, U > 565 | 566 | T << U 567 | 568 | T <<= U 569 |
575 | right_shiftable< T > 576 | 577 | T >> T 578 | 579 | T >>= T 580 |
583 | right_shiftable< T, U > 584 | 585 | T >> U 586 | 587 | T >>= U 588 |
594 | incrementable< T > 595 | 596 | T++ 597 | 598 | ++T 599 |
605 | decrementable< T > 606 | 607 | T-- 608 | 609 | --T 610 |
614 | 615 | The following templates provide common groups of related operations. 616 | For example, since a type which is left shiftable is usually also 617 | right shiftable, the `shiftable` template provides the combined operators 618 | of both. 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 634 | 635 | 641 | 642 | 643 | 644 | 645 | 652 | 653 | 661 | 662 | 669 | 670 | 678 | 679 | 680 | 681 | 682 | 688 | 689 | 696 | 697 | 698 | 699 | 700 | 706 | 707 | 713 | 714 | 715 | 716 | 717 | 723 | 724 | 730 | 731 | 732 | 733 | 734 | 740 | 741 | 747 | 748 | 749 | 750 | 751 | 758 | 759 | 766 | 767 | 774 | 775 | 782 | 783 | 790 | 791 | 792 | 793 | 794 | 800 | 801 | 807 | 808 | 809 | 810 | 811 | 817 | 818 | 819 |
TemplateProvides
629 | totally_ordered< T > 630 | 631 | equality_comparable< T >
632 | less_than_comparable< T > 633 |
636 | totally_ordered< T, U > 637 | 638 | equality_comparable< T, U >
639 | less_than_comparable< T, U > 640 |
646 | commutative_ring< T > 647 | 648 | commutative_addable< T >
649 | subtractable< T >
650 | commutative_multipliable< T > 651 |
654 | commutative_ring< T, U > 655 | 656 | commutative_addable< T, U >
657 | subtractable< T, U >
658 | subtractable_left< T, U >
659 | commutative_multipliable< T, U > 660 |
663 | ring< T > 664 | 665 | commutative_addable< T >
666 | subtractable< T >
667 | multipliable< T > 668 |
671 | ring< T, U > 672 | 673 | commutative_addable< T, U >
674 | subtractable< T, U >
675 | subtractable_left< T, U >
676 | multipliable< T, U > 677 |
683 | field< T > 684 | 685 | commutative_ring< T >
686 | dividable< T > 687 |
690 | field< T, U > 691 | 692 | commutative_ring< T, U >
693 | dividable< T, U >
694 | dividable_left< T, U > 695 |
701 | ordered_commutative_ring< T > 702 | 703 | commutative_ring< T >
704 | totally_ordered< T > 705 |
708 | ordered_commutative_ring< T, U > 709 | 710 | commutative_ring< T, U >
711 | totally_ordered< T, U > 712 |
718 | ordered_ring< T > 719 | 720 | ring< T >
721 | totally_ordered< T > 722 |
725 | ordered_ring< T, U > 726 | 727 | ring< T, U >
728 | totally_ordered< T, U > 729 |
735 | ordered_field< T > 736 | 737 | field< T >
738 | totally_ordered< T > 739 |
742 | ordered_field< T, U > 743 | 744 | field< T, U >
745 | totally_ordered< T, U > 746 |
752 | commutative_bitwise< T > 753 | 754 | commutative_andable< T >
755 | commutative_orable< T >
756 | commutative_xorable< T > 757 |
760 | commutative_bitwise< T, U > 761 | 762 | commutative_andable< T, U >
763 | commutative_orable< T, U >
764 | commutative_xorable< T, U > 765 |
768 | bitwise< T > 769 | 770 | andable< T >
771 | orable< T >
772 | xorable< T > 773 |
776 | bitwise< T, U > 777 | 778 | andable< T, U >
779 | orable< T, U >
780 | xorable< T, U > 781 |
784 | bitwise_left< T, U > 785 | 786 | andable_left< T, U >
787 | orable_left< T, U >
788 | xorable_left< T, U > 789 |
795 | shiftable< T > 796 | 797 | left_shiftable< T >
798 | right_shiftable< T > 799 |
802 | shiftable< T, U > 803 | 804 | left_shiftable< T, U >
805 | right_shiftable< T, U > 806 |
812 | unit_steppable< T > 813 | 814 | incrementable< T >
815 | decrementable< T > 816 |
820 | 821 | ## Commutativity 822 | 823 | For some templates, there are both [commutative](https://en.wikipedia.org/wiki/Commutative_property) 824 | and non-commutative versions available. If the class you are writing is 825 | commutative wrt an operation, you should prefer the commutative template, 826 | i.e., the one which has `commutative_` at the beginning. 827 | 828 | It will be *more efficient* in some cases because it can avoid an 829 | extra temporary for the result and it has *fewer requirements*. 830 | 831 | The one-argument version of the commutative template provides the same 832 | operators as the non-commutative one, but you can see from the result type 833 | in which cases creating a temporary (returning `T`) can be avoided 834 | (returning `T&&`). 835 | 836 | For the two-argument version, `commutative_{OP}< T, U >` provides the operators 837 | of both `{OP}< T, U >` and `{OP}_left< T, U >`, again the return type indicates 838 | those cases where an extra temporary is avoided. 839 | 840 | ## RValue References 841 | 842 | As you can see above, several overloads of some operators return rvalue references. 843 | This helps to eliminate temporaries in more complicated expressions, but some people 844 | consider it dangerous. The argument against returning rvalue references usually 845 | is something like: 846 | 847 | ```c++ 848 | const auto& result = a + b + c; 849 | ``` 850 | 851 | where they expect a temporary to be returned from the expression `a + b + c`, 852 | and the lifetime of the temporary can be extended by binding it to a reference. 853 | 854 | While this would work if an actual temporary value is returned, it does not work with 855 | the second operator `+` returning an rvalue reference to the *intermediate* temporary 856 | created by the first operator `+`. 857 | 858 | I consider the above code bad style that has no place in modern C++. It should be 859 | replaced by 860 | 861 | ```c++ 862 | const auto result = a + b + c; 863 | ``` 864 | 865 | and the problem goes away. Also, if you *expect* an expression to return a temporary 866 | value, but you don't *verify* your assumption, it is your fault for basing your code 867 | on those assumptions. 868 | 869 | There is, however, one problem where the above binding to a references happens behind 870 | the scenes, i.e. without being immediately visible. It may happen if you are using 871 | a range-based for-loop. The problem in this case is not limited to returning rvalue 872 | references, hence you should always make sure that you do not mix any kind of expression 873 | other than directly naming a variable when using a range-based for-loop. Example: 874 | 875 | ```c++ 876 | // instead of this: 877 | for( const auto& e : a + b + c ) { ... } 878 | 879 | // always use something like this: 880 | const auto r = a + b + c; 881 | for( const auto& e : r ) { ... } 882 | ``` 883 | 884 | With all that said, you can disable returning rvalue references by defining 885 | `TAO_OPERATORS_NO_RVALUE_REFERENCE_RESULTS`. If it is set, all operators will 886 | return a value (an rvalue) instead of rvalue references. 887 | 888 | ## constexpr 889 | 890 | All generated comparison operators are `constexpr` by default. 891 | To switch off `constexpr` support simply 892 | 893 | ```c++ 894 | #define TAO_OPERATORS_CONSTEXPR 895 | ``` 896 | 897 | before including ``. 898 | 899 | Note that Visual C++ seems to have some problems with `constexpr` depending 900 | on compile mode (debug/release), etc. and `constexpr` support is therefore 901 | disabled by default. To manually enable it again use 902 | 903 | ```c++ 904 | #define TAO_OPERATORS_CONSTEXPR constexpr 905 | ``` 906 | 907 | before including ``. 908 | 909 | ## noexcept 910 | 911 | For compilers that do not support `noexcept`, the following might be a viable 912 | work-around: 913 | 914 | ```c++ 915 | #include // make sure it's included before the following! 916 | #define noexcept(...) 917 | // you probably also need this for older compilers: 918 | #define TAO_OPERATORS_CONSTEXPR 919 | #include 920 | #undef noexcept 921 | ``` 922 | 923 | ## nodiscard 924 | 925 | When compiling with C++17 or higher, all generated methods are marked `[[nodiscard]]`. 926 | For compilers that do not support `[[nodiscard]]` or when it is causing trouble, 927 | you can disable it defining `TAO_OPERATORS_NODISCARD`: 928 | 929 | ```c++ 930 | #define TAO_OPERATORS_NODISCARD 931 | #include 932 | ``` 933 | 934 | ## Changelog 935 | 936 | ### 1.2.2 937 | 938 | Released 2019-06-04 939 | 940 | * Fix `CMakeLists.txt` version number. 941 | 942 | ### 1.2.1 943 | 944 | Released 2019-06-04 945 | 946 | * Add work-around for MSVC to fix broken EBO in more cases. 947 | 948 | ### 1.2.0 949 | 950 | Released 2019-03-30 951 | 952 | * Add support for `[[nodiscard]]`. 953 | 954 | ### 1.1.1 955 | 956 | Released 2018-06-17 957 | 958 | * Automatic upload of Conan packages on release. 959 | 960 | ### 1.1.0 961 | 962 | Released 2018-06-17 963 | 964 | * Add `constexpr` support for comparison operators. 965 | 966 | ### 1.0.2 967 | 968 | Released 2018-06-05 969 | 970 | * Improve CMake support. 971 | * Conan support. 972 | 973 | ### 1.0.1 974 | 975 | Released 2018-04-23 976 | 977 | * Work-around for MSVC to fix broken EBO. 978 | 979 | ### 1.0.0 980 | 981 | Released 2018-02-13 982 | 983 | * Initial release. 984 | 985 | ## History 986 | 987 | The Art of C++ / Operators is a modernised C++11 rewrite of the [Boost.Operators](http://www.boost.org/doc/libs/1_66_0/libs/utility/operators.htm) library. 988 | 989 | ## License 990 | 991 | The Art of C++ is certified [Open Source](http://www.opensource.org/docs/definition.html) software. It may be used for any purpose, including commercial purposes, at absolutely no cost. It is distributed under the terms of the [MIT license](http://www.opensource.org/licenses/mit-license.html) reproduced here. 992 | 993 | > Copyright (c) 2013-2020 Daniel Frey 994 | > 995 | > Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 996 | > 997 | > The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 998 | > 999 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1000 | -------------------------------------------------------------------------------- /cmake/dummy-config.cmake.in: -------------------------------------------------------------------------------- 1 | # Dummy config file 2 | # When a dependency is added with add_subdirectory, but searched with find_package 3 | 4 | # Redirect to the directory added with add_subdirectory 5 | add_subdirectory(@PROJECT_SOURCE_DIR@ @PROJECT_BINARY_DIR@) 6 | -------------------------------------------------------------------------------- /doc/reference.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 26 | 27 | 28 | 29 | 30 | 39 | 40 | 53 | 54 | 55 | 56 | 57 | 64 | 65 | 73 | 74 | 75 | 76 | 77 | 87 | 88 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 122 | 123 | 139 | 140 | 152 | 153 | 165 | 166 | 179 | 180 | 181 | 182 | 183 | 195 | 196 | 208 | 209 | 222 | 223 | 224 | 225 | 226 | 238 | 239 | 255 | 256 | 268 | 269 | 281 | 282 | 295 | 296 | 297 | 298 | 299 | 311 | 312 | 324 | 325 | 338 | 339 | 340 | 341 | 342 | 354 | 355 | 367 | 368 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 401 | 402 | 418 | 419 | 431 | 432 | 444 | 445 | 458 | 459 | 460 | 461 | 462 | 474 | 475 | 491 | 492 | 504 | 505 | 517 | 518 | 531 | 532 | 533 | 534 | 535 | 547 | 548 | 564 | 565 | 577 | 578 | 590 | 591 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 624 | 625 | 637 | 638 | 639 | 640 | 641 | 653 | 654 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 682 | 683 | 684 | 685 | 686 | 694 | 695 | 696 |
TemplateProvidesRequires
10 | equality_comparable< T > 11 | 12 | bool operator!=( const T& t, const T& t1 ) 13 | 14 | static_cast< bool >( t == t1 ) 15 |
18 | equality_comparable< T, U > 19 | 20 | bool operator!=( const T& t, const U& u )
21 | bool operator==( const U& u, const T& t )
22 | bool operator!=( const U& u, const T& t ) 23 |
24 | static_cast< bool >( t == u ) 25 |
31 | less_than_comparable< T > 32 | 33 | bool operator>( const T& t, const T& t1 )
34 | bool operator<=( const T& t, const T& t1 )
35 | bool operator>=( const T& t, const T& t1 ) 36 |
37 | static_cast< bool >( t < t1 ) 38 |
41 | less_than_comparable< T, U > 42 | 43 | bool operator<=( const T& t, const U& u )
44 | bool operator>=( const T& t, const U& u )
45 | bool operator<( const U& u, const T& t )
46 | bool operator>( const U& u, const T& t )
47 | bool operator<=( const U& u, const T& t )
48 | bool operator>=( const U& u, const T& t ) 49 |
50 | static_cast< bool >( t < u )
51 | static_cast< bool >( t > u ) 52 |
58 | equivalent< T > 59 | 60 | bool operator==( const T& t, const T& t1 ) 61 | 62 | static_cast< bool >( t < t1 ) 63 |
66 | equivalent< T, U > 67 | 68 | bool operator==( const T& t, const U& u ) 69 | 70 | static_cast< bool >( t < u )
71 | static_cast< bool >( t > u ) 72 |
78 | partially_ordered< T > 79 | 80 | bool operator>( const T& t, const T& t1 )
81 | bool operator<=( const T& t, const T& t1 )
82 | bool operator>=( const T& t, const T& t1 ) 83 |
84 | static_cast< bool >( t < t1 )
85 | static_cast< bool >( t == t1 ) 86 |
89 | partially_ordered< T, U > 90 | 91 | bool operator<=( const T& t, const U& u )
92 | bool operator>=( const T& t, const U& u )
93 | bool operator<( const U& u, const T& t )
94 | bool operator>( const U& u, const T& t )
95 | bool operator<=( const U& u, const T& t )
96 | bool operator>=( const U& u, const T& t ) 97 |
98 | static_cast< bool >( t < u )
99 | static_cast< bool >( t > u )
100 | static_cast< bool >( t == u ) 101 |
TemplateProvidesRequires
111 | commutative_addable< T > 112 | 113 | T operator+( const T& t, const T& t1 )
114 | T&& operator+( const T& t, T&& t1 )
115 | T&& operator+( T&& t, const T& t1 )
116 | T&& operator+( T&& t, T&& t1 ) 117 |
118 | T tmp( t )
119 | tmp += t
120 | tmp += std::move( t ) 121 |
124 | commutative_addable< T, U > 125 | 126 | T operator+( const T& t, const U& u )
127 | T operator+( const T& t, U&& u )
128 | T&& operator+( T&& t, const U& u )
129 | T&& operator+( T&& t, U&& u )
130 | T operator+( const U& u, const T& t )
131 | T&& operator+( const U& u, T&& t )
132 | T operator+( U&& u, const T& t )
133 | T&& operator+( U&& u, T&& t ) 134 |
135 | T tmp( t )
136 | tmp += u
137 | tmp += std::move( u ) 138 |
141 | addable< T > 142 | 143 | T operator+( const T& t, const T& t1 )
144 | T operator+( const T& t, T&& t1 )
145 | T&& operator+( T&& t, const T& t1 )
146 | T&& operator+( T&& t, T&& t1 ) 147 |
148 | T tmp( t )
149 | tmp += t
150 | tmp += std::move( t ) 151 |
154 | addable< T, U > 155 | 156 | T operator+( const T& t, const U& u )
157 | T operator+( const T& t, U&& u )
158 | T&& operator+( T&& t, const U& u )
159 | T&& operator+( T&& t, U&& u ) 160 |
161 | T tmp( t )
162 | tmp += u
163 | tmp += std::move( u ) 164 |
167 | addable_left< T, U > 168 | 169 | T operator+( const U& u, const T& t )
170 | T operator+( const U& u, T&& t )
171 | T operator+( U&& u, const T& t )
172 | T operator+( U&& u, T&& t ) 173 |
174 | T tmp( u )
175 | T tmp( std::move( u ) )
176 | tmp += t
177 | tmp += std::move( t ) 178 |
184 | subtractable< T > 185 | 186 | T operator-( const T& t, const T& t1 )
187 | T operator-( const T& t, T&& t1 )
188 | T&& operator-( T&& t, const T& t1 )
189 | T&& operator-( T&& t, T&& t1 ) 190 |
191 | T tmp( t )
192 | tmp -= t
193 | tmp -= std::move( t ) 194 |
197 | subtractable< T, U > 198 | 199 | T operator-( const T& t, const U& u )
200 | T operator-( const T& t, U&& u )
201 | T&& operator-( T&& t, const U& u )
202 | T&& operator-( T&& t, U&& u ) 203 |
204 | T tmp( t )
205 | tmp -= u
206 | tmp -= std::move( u ) 207 |
210 | subtractable_left< T, U > 211 | 212 | T operator-( const U& u, const T& t )
213 | T operator-( const U& u, T&& t )
214 | T operator-( U&& u, const T& t )
215 | T operator-( U&& u, T&& t ) 216 |
217 | T tmp( u )
218 | T tmp( std::move( u ) )
219 | tmp -= t
220 | tmp -= std::move( t ) 221 |
227 | commutative_multipliable< T > 228 | 229 | T operator*( const T& t, const T& t1 )
230 | T&& operator*( const T& t, T&& t1 )
231 | T&& operator*( T&& t, const T& t1 )
232 | T&& operator*( T&& t, T&& t1 ) 233 |
234 | T tmp( t )
235 | tmp *= t
236 | tmp *= std::move( t ) 237 |
240 | commutative_multipliable< T, U > 241 | 242 | T operator*( const T& t, const U& u )
243 | T operator*( const T& t, U&& u )
244 | T&& operator*( T&& t, const U& u )
245 | T&& operator*( T&& t, U&& u )
246 | T operator*( const U& u, const T& t )
247 | T&& operator*( const U& u, T&& t )
248 | T operator*( U&& u, const T& t )
249 | T&& operator*( U&& u, T&& t ) 250 |
251 | T tmp( t )
252 | tmp *= u
253 | tmp *= std::move( u ) 254 |
257 | multipliable< T > 258 | 259 | T operator*( const T& t, const T& t1 )
260 | T operator*( const T& t, T&& t1 )
261 | T&& operator*( T&& t, const T& t1 )
262 | T&& operator*( T&& t, T&& t1 ) 263 |
264 | T tmp( t )
265 | tmp *= t
266 | tmp *= std::move( t ) 267 |
270 | multipliable< T, U > 271 | 272 | T operator*( const T& t, const U& u )
273 | T operator*( const T& t, U&& u )
274 | T&& operator*( T&& t, const U& u )
275 | T&& operator*( T&& t, U&& u ) 276 |
277 | T tmp( t )
278 | tmp *= u
279 | tmp *= std::move( u ) 280 |
283 | multipliable_left< T, U > 284 | 285 | T operator*( const U& u, const T& t )
286 | T operator*( const U& u, T&& t )
287 | T operator*( U&& u, const T& t )
288 | T operator*( U&& u, T&& t ) 289 |
290 | T tmp( u )
291 | T tmp( std::move( u ) )
292 | tmp *= t
293 | tmp *= std::move( t ) 294 |
300 | dividable< T > 301 | 302 | T operator/( const T& t, const T& t1 )
303 | T operator/( const T& t, T&& t1 )
304 | T&& operator/( T&& t, const T& t1 )
305 | T&& operator/( T&& t, T&& t1 ) 306 |
307 | T tmp( t )
308 | tmp /= t
309 | tmp /= std::move( t ) 310 |
313 | dividable< T, U > 314 | 315 | T operator/( const T& t, const U& u )
316 | T operator/( const T& t, U&& u )
317 | T&& operator/( T&& t, const U& u )
318 | T&& operator/( T&& t, U&& u ) 319 |
320 | T tmp( t )
321 | tmp /= u
322 | tmp /= std::move( u ) 323 |
326 | dividable_left< T, U > 327 | 328 | T operator/( const U& u, const T& t )
329 | T operator/( const U& u, T&& t )
330 | T operator/( U&& u, const T& t )
331 | T operator/( U&& u, T&& t ) 332 |
333 | T tmp( u )
334 | T tmp( std::move( u ) )
335 | tmp /= t
336 | tmp /= std::move( t ) 337 |
343 | modable< T > 344 | 345 | T operator%( const T& t, const T& t1 )
346 | T operator%( const T& t, T&& t1 )
347 | T&& operator%( T&& t, const T& t1 )
348 | T&& operator%( T&& t, T&& t1 ) 349 |
350 | T tmp( t )
351 | tmp %= t
352 | tmp %= std::move( t ) 353 |
356 | modable< T, U > 357 | 358 | T operator%( const T& t, const U& u )
359 | T operator%( const T& t, U&& u )
360 | T&& operator%( T&& t, const U& u )
361 | T&& operator%( T&& t, U&& u ) 362 |
363 | T tmp( t )
364 | tmp %= u
365 | tmp %= std::move( u ) 366 |
369 | modable_left< T, U > 370 | 371 | T operator%( const U& u, const T& t )
372 | T operator%( const U& u, T&& t )
373 | T operator%( U&& u, const T& t )
374 | T operator%( U&& u, T&& t ) 375 |
376 | T tmp( u )
377 | T tmp( std::move( u ) )
378 | tmp %= t
379 | tmp %= std::move( t ) 380 |
TemplateProvidesRequires
390 | commutative_andable< T > 391 | 392 | T operator&( const T& t, const T& t1 )
393 | T&& operator&( const T& t, T&& t1 )
394 | T&& operator&( T&& t, const T& t1 )
395 | T&& operator&( T&& t, T&& t1 ) 396 |
397 | T tmp( t )
398 | tmp &= t
399 | tmp &= std::move( t ) 400 |
403 | commutative_andable< T, U > 404 | 405 | T operator&( const T& t, const U& u )
406 | T operator&( const T& t, U&& u )
407 | T&& operator&( T&& t, const U& u )
408 | T&& operator&( T&& t, U&& u )
409 | T operator&( const U& u, const T& t )
410 | T&& operator&( const U& u, T&& t )
411 | T operator&( U&& u, const T& t )
412 | T&& operator&( U&& u, T&& t ) 413 |
414 | T tmp( t )
415 | tmp &= u
416 | tmp &= std::move( u ) 417 |
420 | andable< T > 421 | 422 | T operator&( const T& t, const T& t1 )
423 | T operator&( const T& t, T&& t1 )
424 | T&& operator&( T&& t, const T& t1 )
425 | T&& operator&( T&& t, T&& t1 ) 426 |
427 | T tmp( t )
428 | tmp &= t
429 | tmp &= std::move( t ) 430 |
433 | andable< T, U > 434 | 435 | T operator&( const T& t, const U& u )
436 | T operator&( const T& t, U&& u )
437 | T&& operator&( T&& t, const U& u )
438 | T&& operator&( T&& t, U&& u ) 439 |
440 | T tmp( t )
441 | tmp &= u
442 | tmp &= std::move( u ) 443 |
446 | andable_left< T, U > 447 | 448 | T operator&( const U& u, const T& t )
449 | T operator&( const U& u, T&& t )
450 | T operator&( U&& u, const T& t )
451 | T operator&( U&& u, T&& t ) 452 |
453 | T tmp( u )
454 | T tmp( std::move( u ) )
455 | tmp &= t
456 | tmp &= std::move( t ) 457 |
463 | commutative_orable< T > 464 | 465 | T operator|( const T& t, const T& t1 )
466 | T&& operator|( const T& t, T&& t1 )
467 | T&& operator|( T&& t, const T& t1 )
468 | T&& operator|( T&& t, T&& t1 ) 469 |
470 | T tmp( t )
471 | tmp |= t
472 | tmp |= std::move( t ) 473 |
476 | commutative_orable< T, U > 477 | 478 | T operator|( const T& t, const U& u )
479 | T operator|( const T& t, U&& u )
480 | T&& operator|( T&& t, const U& u )
481 | T&& operator|( T&& t, U&& u )
482 | T operator|( const U& u, const T& t )
483 | T&& operator|( const U& u, T&& t )
484 | T operator|( U&& u, const T& t )
485 | T&& operator|( U&& u, T&& t ) 486 |
487 | T tmp( t )
488 | tmp |= u
489 | tmp |= std::move( u ) 490 |
493 | orable< T > 494 | 495 | T operator|( const T& t, const T& t1 )
496 | T operator|( const T& t, T&& t1 )
497 | T&& operator|( T&& t, const T& t1 )
498 | T&& operator|( T&& t, T&& t1 ) 499 |
500 | T tmp( t )
501 | tmp |= t
502 | tmp |= std::move( t ) 503 |
506 | orable< T, U > 507 | 508 | T operator|( const T& t, const U& u )
509 | T operator|( const T& t, U&& u )
510 | T&& operator|( T&& t, const U& u )
511 | T&& operator|( T&& t, U&& u ) 512 |
513 | T tmp( t )
514 | tmp |= u
515 | tmp |= std::move( u ) 516 |
519 | orable_left< T, U > 520 | 521 | T operator|( const U& u, const T& t )
522 | T operator|( const U& u, T&& t )
523 | T operator|( U&& u, const T& t )
524 | T operator|( U&& u, T&& t ) 525 |
526 | T tmp( u )
527 | T tmp( std::move( u ) )
528 | tmp |= t
529 | tmp |= std::move( t ) 530 |
536 | commutative_xorable< T > 537 | 538 | T operator^( const T& t, const T& t1 )
539 | T&& operator^( const T& t, T&& t1 )
540 | T&& operator^( T&& t, const T& t1 )
541 | T&& operator^( T&& t, T&& t1 ) 542 |
543 | T tmp( t )
544 | tmp ^= t
545 | tmp ^= std::move( t ) 546 |
549 | commutative_xorable< T, U > 550 | 551 | T operator^( const T& t, const U& u )
552 | T operator^( const T& t, U&& u )
553 | T&& operator^( T&& t, const U& u )
554 | T&& operator^( T&& t, U&& u )
555 | T operator^( const U& u, const T& t )
556 | T&& operator^( const U& u, T&& t )
557 | T operator^( U&& u, const T& t )
558 | T&& operator^( U&& u, T&& t ) 559 |
560 | T tmp( t )
561 | tmp ^= u
562 | tmp ^= std::move( u ) 563 |
566 | xorable< T > 567 | 568 | T operator^( const T& t, const T& t1 )
569 | T operator^( const T& t, T&& t1 )
570 | T&& operator^( T&& t, const T& t1 )
571 | T&& operator^( T&& t, T&& t1 ) 572 |
573 | T tmp( t )
574 | tmp ^= t
575 | tmp ^= std::move( t ) 576 |
579 | xorable< T, U > 580 | 581 | T operator^( const T& t, const U& u )
582 | T operator^( const T& t, U&& u )
583 | T&& operator^( T&& t, const U& u )
584 | T&& operator^( T&& t, U&& u ) 585 |
586 | T tmp( t )
587 | tmp ^= u
588 | tmp ^= std::move( u ) 589 |
592 | xorable_left< T, U > 593 | 594 | T operator^( const U& u, const T& t )
595 | T operator^( const U& u, T&& t )
596 | T operator^( U&& u, const T& t )
597 | T operator^( U&& u, T&& t ) 598 |
599 | T tmp( u )
600 | T tmp( std::move( u ) )
601 | tmp ^= t
602 | tmp ^= std::move( t ) 603 |
TemplateProvidesRequires
613 | left_shiftable< T > 614 | 615 | T operator<<( const T& t, const T& t1 )
616 | T operator<<( const T& t, T&& t1 )
617 | T&& operator<<( T&& t, const T& t1 )
618 | T&& operator<<( T&& t, T&& t1 ) 619 |
620 | T tmp( t )
621 | tmp <<= t
622 | tmp <<= std::move( t ) 623 |
626 | left_shiftable< T, U > 627 | 628 | T operator<<( const T& t, const U& u )
629 | T operator<<( const T& t, U&& u )
630 | T&& operator<<( T&& t, const U& u )
631 | T&& operator<<( T&& t, U&& u ) 632 |
633 | T tmp( t )
634 | tmp <<= u
635 | tmp <<= std::move( u ) 636 |
642 | right_shiftable< T > 643 | 644 | T operator>>( const T& t, const T& t1 )
645 | T operator>>( const T& t, T&& t1 )
646 | T&& operator>>( T&& t, const T& t1 )
647 | T&& operator>>( T&& t, T&& t1 ) 648 |
649 | T tmp( t )
650 | tmp >>= t
651 | tmp >>= std::move( t ) 652 |
655 | right_shiftable< T, U > 656 | 657 | T operator>>( const T& t, const U& u )
658 | T operator>>( const T& t, U&& u )
659 | T&& operator>>( T&& t, const U& u )
660 | T&& operator>>( T&& t, U&& u ) 661 |
662 | T tmp( t )
663 | tmp >>= u
664 | tmp >>= std::move( u ) 665 |
TemplateProvidesRequires
675 | incrementable< T > 676 | 677 | T operator++( T& t, int ) 678 | 679 | T tmp( t )
680 | ++t 681 |
687 | decrementable< T > 688 | 689 | T operator--( T& t, int ) 690 | 691 | T tmp( t )
692 | --t 693 |
697 | -------------------------------------------------------------------------------- /include/tao/operators.hpp: -------------------------------------------------------------------------------- 1 | // The Art of C++ / Operators 2 | // Copyright (c) 2013-2020 Daniel Frey 3 | // Please see LICENSE for license or visit https://github.com/taocpp/operators/ 4 | 5 | #ifndef TAO_OPERATORS_HPP 6 | #define TAO_OPERATORS_HPP 7 | 8 | #include 9 | 10 | #ifndef TAO_OPERATORS_BROKEN_EBO 11 | #if defined( _MSC_VER ) && !defined( __clang__ ) 12 | #define TAO_OPERATORS_BROKEN_EBO __declspec( empty_bases ) 13 | #else 14 | #define TAO_OPERATORS_BROKEN_EBO 15 | #endif 16 | #endif 17 | 18 | #ifndef TAO_OPERATORS_CONSTEXPR 19 | #if defined( _MSC_VER ) && !defined( __clang__ ) 20 | #define TAO_OPERATORS_CONSTEXPR 21 | #else 22 | #define TAO_OPERATORS_CONSTEXPR constexpr // NOLINT 23 | #endif 24 | #endif 25 | 26 | #ifndef TAO_OPERATORS_NODISCARD 27 | #if( __cplusplus >= 201703L ) 28 | #define TAO_OPERATORS_NODISCARD [[nodiscard]] 29 | #else 30 | #define TAO_OPERATORS_NODISCARD 31 | #endif 32 | #endif 33 | 34 | #ifndef TAO_OPERATORS_NO_RVALUE_REFERENCE_RESULTS 35 | #define TAO_OPERATORS_BASIC_OP( name, op ) \ 36 | template< typename T, typename U = T > \ 37 | class TAO_OPERATORS_BROKEN_EBO name /* NOLINT */ \ 38 | { \ 39 | friend T operator op( const T& lhs, const U& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 40 | { \ 41 | T nrv( lhs ); \ 42 | nrv op## = rhs; \ 43 | return nrv; \ 44 | } \ 45 | \ 46 | friend T operator op( const T& lhs, U&& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 47 | { \ 48 | T nrv( lhs ); \ 49 | nrv op## = std::move( rhs ); \ 50 | return nrv; \ 51 | } \ 52 | \ 53 | friend T&& operator op( T&& lhs, const U& rhs ) noexcept( noexcept( lhs op## = rhs ) ) \ 54 | { \ 55 | lhs op## = rhs; \ 56 | return std::move( lhs ); \ 57 | } \ 58 | \ 59 | friend T&& operator op( T&& lhs, U&& rhs ) noexcept( noexcept( lhs op## = std::move( rhs ) ) ) \ 60 | { \ 61 | lhs op## = std::move( rhs ); \ 62 | return std::move( lhs ); \ 63 | } \ 64 | } 65 | #else 66 | #define TAO_OPERATORS_BASIC_OP( name, op ) \ 67 | template< typename T, typename U = T > \ 68 | class TAO_OPERATORS_BROKEN_EBO name \ 69 | { \ 70 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, const U& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 71 | { \ 72 | T nrv( lhs ); \ 73 | nrv op## = rhs; \ 74 | return nrv; \ 75 | } \ 76 | \ 77 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, U&& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 78 | { \ 79 | T nrv( lhs ); \ 80 | nrv op## = std::move( rhs ); \ 81 | return nrv; \ 82 | } \ 83 | \ 84 | TAO_OPERATORS_NODISCARD friend T operator op( T&& lhs, const U& rhs ) noexcept( noexcept( T( std::move( lhs ) ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 85 | { \ 86 | T nrv( std::move( lhs ) ); \ 87 | nrv op## = rhs; \ 88 | return nrv; \ 89 | } \ 90 | \ 91 | TAO_OPERATORS_NODISCARD friend T operator op( T&& lhs, U&& rhs ) noexcept( noexcept( T( std::move( lhs ) ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 92 | { \ 93 | T nrv( std::move( lhs ) ); \ 94 | nrv op## = std::move( rhs ); \ 95 | return nrv; \ 96 | } \ 97 | } 98 | #endif 99 | 100 | #define TAO_OPERATORS_BASIC_OP_LEFT( name, op ) \ 101 | template< typename T, typename U > \ 102 | class TAO_OPERATORS_BROKEN_EBO name##_left \ 103 | { \ 104 | TAO_OPERATORS_NODISCARD friend T operator op( const U& lhs, const T& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 105 | { \ 106 | T nrv( lhs ); \ 107 | nrv op## = rhs; \ 108 | return nrv; \ 109 | } \ 110 | \ 111 | TAO_OPERATORS_NODISCARD friend T operator op( const U& lhs, T&& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 112 | { \ 113 | T nrv( lhs ); \ 114 | nrv op## = std::move( rhs ); \ 115 | return nrv; \ 116 | } \ 117 | \ 118 | TAO_OPERATORS_NODISCARD friend T operator op( U&& lhs, const T& rhs ) noexcept( noexcept( T( std::move( lhs ) ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 119 | { \ 120 | T nrv( std::move( lhs ) ); \ 121 | nrv op## = rhs; \ 122 | return nrv; \ 123 | } \ 124 | \ 125 | TAO_OPERATORS_NODISCARD friend T operator op( U&& lhs, T&& rhs ) noexcept( noexcept( T( std::move( lhs ) ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 126 | { \ 127 | T nrv( std::move( lhs ) ); \ 128 | nrv op## = std::move( rhs ); \ 129 | return nrv; \ 130 | } \ 131 | } 132 | 133 | #ifndef TAO_OPERATORS_NO_RVALUE_REFERENCE_RESULTS 134 | #define TAO_OPERATORS_BASIC_OP_COMMUTATIVE( name, op ) \ 135 | template< typename T, typename U = T > \ 136 | class TAO_OPERATORS_BROKEN_EBO commutative_##name \ 137 | { \ 138 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, const U& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 139 | { \ 140 | T nrv( lhs ); \ 141 | nrv op## = rhs; \ 142 | return nrv; \ 143 | } \ 144 | \ 145 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, U&& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 146 | { \ 147 | T nrv( lhs ); \ 148 | nrv op## = std::move( rhs ); \ 149 | return nrv; \ 150 | } \ 151 | \ 152 | TAO_OPERATORS_NODISCARD friend T&& operator op( T&& lhs, const U& rhs ) noexcept( noexcept( lhs op## = rhs ) ) \ 153 | { \ 154 | lhs op## = rhs; \ 155 | return std::move( lhs ); \ 156 | } \ 157 | \ 158 | TAO_OPERATORS_NODISCARD friend T&& operator op( T&& lhs, U&& rhs ) noexcept( noexcept( lhs op## = std::move( rhs ) ) ) \ 159 | { \ 160 | lhs op## = std::move( rhs ); \ 161 | return std::move( lhs ); \ 162 | } \ 163 | \ 164 | TAO_OPERATORS_NODISCARD friend T operator op( const U& lhs, const T& rhs ) noexcept( noexcept( T( rhs ), std::declval< T& >() op## = lhs, T( std::declval< T& >() ) ) ) \ 165 | { \ 166 | T nrv( rhs ); \ 167 | nrv op## = lhs; \ 168 | return nrv; \ 169 | } \ 170 | \ 171 | TAO_OPERATORS_NODISCARD friend T&& operator op( const U& lhs, T&& rhs ) noexcept( noexcept( rhs op## = lhs ) ) \ 172 | { \ 173 | rhs op## = lhs; \ 174 | return std::move( rhs ); \ 175 | } \ 176 | \ 177 | TAO_OPERATORS_NODISCARD friend T operator op( U&& lhs, const T& rhs ) noexcept( noexcept( T( rhs ), std::declval< T& >() op## = std::move( lhs ) ) ) \ 178 | { \ 179 | T nrv( rhs ); \ 180 | nrv op## = std::move( lhs ); \ 181 | return nrv; \ 182 | } \ 183 | \ 184 | TAO_OPERATORS_NODISCARD friend T&& operator op( U&& lhs, T&& rhs ) noexcept( noexcept( rhs op## = std::move( lhs ) ) ) \ 185 | { \ 186 | rhs op## = std::move( lhs ); \ 187 | return std::move( rhs ); \ 188 | } \ 189 | }; \ 190 | \ 191 | template< typename T > \ 192 | class TAO_OPERATORS_BROKEN_EBO commutative_##name< T > \ 193 | { \ 194 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, const T& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 195 | { \ 196 | T nrv( lhs ); \ 197 | nrv op## = rhs; \ 198 | return nrv; \ 199 | } \ 200 | \ 201 | TAO_OPERATORS_NODISCARD friend T&& operator op( const T& lhs, T&& rhs ) noexcept( noexcept( rhs op## = lhs ) ) \ 202 | { \ 203 | rhs op## = lhs; \ 204 | return std::move( rhs ); \ 205 | } \ 206 | \ 207 | TAO_OPERATORS_NODISCARD friend T&& operator op( T&& lhs, const T& rhs ) noexcept( noexcept( lhs op## = rhs ) ) \ 208 | { \ 209 | lhs op## = rhs; \ 210 | return std::move( lhs ); \ 211 | } \ 212 | \ 213 | TAO_OPERATORS_NODISCARD friend T&& operator op( T&& lhs, T&& rhs ) noexcept( noexcept( lhs op## = std::move( rhs ) ) ) \ 214 | { \ 215 | lhs op## = std::move( rhs ); \ 216 | return std::move( lhs ); \ 217 | } \ 218 | } 219 | #else 220 | #define TAO_OPERATORS_BASIC_OP_COMMUTATIVE( name, op ) \ 221 | template< typename T, typename U = T > \ 222 | class TAO_OPERATORS_BROKEN_EBO commutative_##name \ 223 | { \ 224 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, const U& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 225 | { \ 226 | T nrv( lhs ); \ 227 | nrv op## = rhs; \ 228 | return nrv; \ 229 | } \ 230 | \ 231 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, U&& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 232 | { \ 233 | T nrv( lhs ); \ 234 | nrv op## = std::move( rhs ); \ 235 | return nrv; \ 236 | } \ 237 | \ 238 | TAO_OPERATORS_NODISCARD friend T operator op( T&& lhs, const U& rhs ) noexcept( noexcept( T( std::move( lhs ) ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 239 | { \ 240 | T nrv( std::move( lhs ) ); \ 241 | nrv op## = rhs; \ 242 | return nrv; \ 243 | } \ 244 | \ 245 | TAO_OPERATORS_NODISCARD friend T operator op( T&& lhs, U&& rhs ) noexcept( noexcept( T( std::move( lhs ) ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 246 | { \ 247 | T nrv( std::move( lhs ) ); \ 248 | nrv op## = std::move( rhs ); \ 249 | return nrv; \ 250 | } \ 251 | \ 252 | TAO_OPERATORS_NODISCARD friend T operator op( const U& lhs, const T& rhs ) noexcept( noexcept( T( rhs ), std::declval< T& >() op## = lhs, T( std::declval< T& >() ) ) ) \ 253 | { \ 254 | T nrv( rhs ); \ 255 | nrv op## = lhs; \ 256 | return nrv; \ 257 | } \ 258 | \ 259 | TAO_OPERATORS_NODISCARD friend T operator op( const U& lhs, T&& rhs ) noexcept( noexcept( T( std::move( rhs ) ), std::declval< T& >() op## = lhs, T( std::declval< T& >() ) ) ) \ 260 | { \ 261 | T nrv( std::move( rhs ) ); \ 262 | nrv op## = lhs; \ 263 | return nrv; \ 264 | } \ 265 | \ 266 | TAO_OPERATORS_NODISCARD friend T operator op( U&& lhs, const T& rhs ) noexcept( noexcept( T( rhs ), std::declval< T& >() op## = std::move( lhs ) ) ) \ 267 | { \ 268 | T nrv( rhs ); \ 269 | nrv op## = std::move( lhs ); \ 270 | return nrv; \ 271 | } \ 272 | \ 273 | TAO_OPERATORS_NODISCARD friend T operator op( U&& lhs, T&& rhs ) noexcept( noexcept( T( std::move( rhs ) ), std::declval< T& >() op## = std::move( lhs ) ) ) \ 274 | { \ 275 | T nrv( std::move( rhs ) ); \ 276 | nrv op## = std::move( lhs ); \ 277 | return nrv; \ 278 | } \ 279 | }; \ 280 | \ 281 | template< typename T > \ 282 | class TAO_OPERATORS_BROKEN_EBO commutative_##name< T > \ 283 | { \ 284 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, const T& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 285 | { \ 286 | T nrv( lhs ); \ 287 | nrv op## = rhs; \ 288 | return nrv; \ 289 | } \ 290 | \ 291 | TAO_OPERATORS_NODISCARD friend T operator op( const T& lhs, T&& rhs ) noexcept( noexcept( T( lhs ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 292 | { \ 293 | T nrv( lhs ); \ 294 | nrv op## = std::move( rhs ); \ 295 | return nrv; \ 296 | } \ 297 | \ 298 | TAO_OPERATORS_NODISCARD friend T operator op( T&& lhs, const T& rhs ) noexcept( noexcept( T( std::move( lhs ) ), std::declval< T& >() op## = rhs, T( std::declval< T& >() ) ) ) \ 299 | { \ 300 | T nrv( std::move( lhs ) ); \ 301 | nrv op## = rhs; \ 302 | return nrv; \ 303 | } \ 304 | \ 305 | TAO_OPERATORS_NODISCARD friend T operator op( T&& lhs, T&& rhs ) noexcept( noexcept( T( std::move( lhs ) ), std::declval< T& >() op## = std::move( rhs ), T( std::declval< T& >() ) ) ) \ 306 | { \ 307 | T nrv( std::move( lhs ) ); \ 308 | nrv op## = std::move( rhs ); \ 309 | return nrv; \ 310 | } \ 311 | } 312 | #endif 313 | 314 | namespace tao 315 | { 316 | namespace operators 317 | { 318 | template< typename T, typename U = T > 319 | class TAO_OPERATORS_BROKEN_EBO equality_comparable 320 | { 321 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator!=( const T& lhs, const U& rhs ) noexcept( noexcept( static_cast< bool >( lhs == rhs ) ) ) 322 | { 323 | return !static_cast< bool >( lhs == rhs ); 324 | } 325 | 326 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator==( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs == lhs ) ) ) 327 | { 328 | return static_cast< bool >( rhs == lhs ); 329 | } 330 | 331 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator!=( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs != lhs ) ) ) 332 | { 333 | return static_cast< bool >( rhs != lhs ); 334 | } 335 | }; 336 | 337 | template< typename T > 338 | class TAO_OPERATORS_BROKEN_EBO equality_comparable< T > 339 | { 340 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator!=( const T& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( lhs == rhs ) ) ) 341 | { 342 | return !static_cast< bool >( lhs == rhs ); 343 | } 344 | }; 345 | 346 | template< typename T, typename U = T > 347 | class TAO_OPERATORS_BROKEN_EBO less_than_comparable 348 | { 349 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator<=( const T& lhs, const U& rhs ) noexcept( noexcept( static_cast< bool >( lhs > rhs ) ) ) 350 | { 351 | return !static_cast< bool >( lhs > rhs ); 352 | } 353 | 354 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>=( const T& lhs, const U& rhs ) noexcept( noexcept( static_cast< bool >( lhs < rhs ) ) ) 355 | { 356 | return !static_cast< bool >( lhs < rhs ); 357 | } 358 | 359 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator<( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs > lhs ) ) ) 360 | { 361 | return static_cast< bool >( rhs > lhs ); 362 | } 363 | 364 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs < lhs ) ) ) 365 | { 366 | return static_cast< bool >( rhs < lhs ); 367 | } 368 | 369 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator<=( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs >= lhs ) ) ) 370 | { 371 | return static_cast< bool >( rhs >= lhs ); 372 | } 373 | 374 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>=( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs <= lhs ) ) ) 375 | { 376 | return static_cast< bool >( rhs <= lhs ); 377 | } 378 | }; 379 | 380 | template< typename T > 381 | class TAO_OPERATORS_BROKEN_EBO less_than_comparable< T > 382 | { 383 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>( const T& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs < lhs ) ) ) 384 | { 385 | return static_cast< bool >( rhs < lhs ); 386 | } 387 | 388 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator<=( const T& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs < lhs ) ) ) 389 | { 390 | return !static_cast< bool >( rhs < lhs ); 391 | } 392 | 393 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>=( const T& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( lhs < rhs ) ) ) 394 | { 395 | return !static_cast< bool >( lhs < rhs ); 396 | } 397 | }; 398 | 399 | template< typename T, typename U = T > 400 | class TAO_OPERATORS_BROKEN_EBO totally_ordered 401 | : less_than_comparable< T, U >, 402 | equality_comparable< T, U > 403 | { 404 | }; 405 | 406 | template< typename T, typename U = T > 407 | class TAO_OPERATORS_BROKEN_EBO equivalent 408 | { 409 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator==( const T& lhs, const U& rhs ) noexcept( noexcept( static_cast< bool >( lhs < rhs ), static_cast< bool >( lhs > rhs ) ) ) 410 | { 411 | return !static_cast< bool >( lhs < rhs ) && !static_cast< bool >( lhs > rhs ); 412 | } 413 | }; 414 | 415 | template< typename T > 416 | class TAO_OPERATORS_BROKEN_EBO equivalent< T > 417 | { 418 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator==( const T& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( lhs < rhs ) ) ) 419 | { 420 | return !static_cast< bool >( lhs < rhs ) && !static_cast< bool >( rhs < lhs ); 421 | } 422 | }; 423 | 424 | template< typename T, typename U = T > 425 | class TAO_OPERATORS_BROKEN_EBO partially_ordered 426 | { 427 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator<=( const T& lhs, const U& rhs ) noexcept( noexcept( static_cast< bool >( lhs < rhs ), static_cast< bool >( lhs == rhs ) ) ) 428 | { 429 | return static_cast< bool >( lhs < rhs ) || static_cast< bool >( lhs == rhs ); 430 | } 431 | 432 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>=( const T& lhs, const U& rhs ) noexcept( noexcept( static_cast< bool >( lhs > rhs ), static_cast< bool >( lhs == rhs ) ) ) 433 | { 434 | return static_cast< bool >( lhs > rhs ) || static_cast< bool >( lhs == rhs ); 435 | } 436 | 437 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator<( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs > lhs ) ) ) 438 | { 439 | return static_cast< bool >( rhs > lhs ); 440 | } 441 | 442 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs < lhs ) ) ) 443 | { 444 | return static_cast< bool >( rhs < lhs ); 445 | } 446 | 447 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator<=( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs >= lhs ) ) ) 448 | { 449 | return static_cast< bool >( rhs >= lhs ); 450 | } 451 | 452 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>=( const U& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs <= lhs ) ) ) 453 | { 454 | return static_cast< bool >( rhs <= lhs ); 455 | } 456 | }; 457 | 458 | template< typename T > 459 | class TAO_OPERATORS_BROKEN_EBO partially_ordered< T > 460 | { 461 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>( const T& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs < lhs ) ) ) 462 | { 463 | return static_cast< bool >( rhs < lhs ); 464 | } 465 | 466 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator<=( const T& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( lhs < rhs ), static_cast< bool >( lhs == rhs ) ) ) 467 | { 468 | return static_cast< bool >( lhs < rhs ) || static_cast< bool >( lhs == rhs ); 469 | } 470 | 471 | TAO_OPERATORS_NODISCARD friend TAO_OPERATORS_CONSTEXPR bool operator>=( const T& lhs, const T& rhs ) noexcept( noexcept( static_cast< bool >( rhs < lhs ), static_cast< bool >( lhs == rhs ) ) ) 472 | { 473 | return static_cast< bool >( rhs < lhs ) || static_cast< bool >( lhs == rhs ); 474 | } 475 | }; 476 | 477 | TAO_OPERATORS_BASIC_OP( addable, +); 478 | TAO_OPERATORS_BASIC_OP_LEFT( addable, +); 479 | TAO_OPERATORS_BASIC_OP_COMMUTATIVE( addable, +); 480 | 481 | TAO_OPERATORS_BASIC_OP( subtractable, -); 482 | TAO_OPERATORS_BASIC_OP_LEFT( subtractable, -); 483 | 484 | TAO_OPERATORS_BASIC_OP( multipliable, * ); 485 | TAO_OPERATORS_BASIC_OP_LEFT( multipliable, * ); 486 | TAO_OPERATORS_BASIC_OP_COMMUTATIVE( multipliable, * ); 487 | 488 | TAO_OPERATORS_BASIC_OP( dividable, / ); 489 | TAO_OPERATORS_BASIC_OP_LEFT( dividable, / ); 490 | 491 | TAO_OPERATORS_BASIC_OP( modable, % ); 492 | TAO_OPERATORS_BASIC_OP_LEFT( modable, % ); 493 | 494 | template< typename T, typename U = T > 495 | class TAO_OPERATORS_BROKEN_EBO ring 496 | : commutative_addable< T, U >, 497 | subtractable< T, U >, 498 | subtractable_left< T, U >, 499 | multipliable< T, U > 500 | { 501 | }; 502 | 503 | template< typename T > 504 | class TAO_OPERATORS_BROKEN_EBO ring< T > 505 | : commutative_addable< T >, 506 | subtractable< T >, 507 | multipliable< T > 508 | { 509 | }; 510 | 511 | template< typename T, typename U = T > 512 | class TAO_OPERATORS_BROKEN_EBO ordered_ring 513 | : ring< T, U >, 514 | totally_ordered< T, U > 515 | { 516 | }; 517 | 518 | template< typename T, typename U = T > 519 | class TAO_OPERATORS_BROKEN_EBO commutative_ring 520 | : commutative_addable< T, U >, 521 | subtractable< T, U >, 522 | subtractable_left< T, U >, 523 | commutative_multipliable< T, U > 524 | { 525 | }; 526 | 527 | template< typename T > 528 | class TAO_OPERATORS_BROKEN_EBO commutative_ring< T > 529 | : commutative_addable< T >, 530 | subtractable< T >, 531 | commutative_multipliable< T > 532 | { 533 | }; 534 | 535 | template< typename T, typename U = T > 536 | class TAO_OPERATORS_BROKEN_EBO ordered_commutative_ring 537 | : commutative_ring< T, U >, 538 | totally_ordered< T, U > 539 | { 540 | }; 541 | 542 | template< typename T, typename U = T > 543 | class TAO_OPERATORS_BROKEN_EBO field 544 | : commutative_ring< T, U >, 545 | dividable< T, U >, 546 | dividable_left< T, U > 547 | { 548 | }; 549 | 550 | template< typename T > 551 | class TAO_OPERATORS_BROKEN_EBO field< T > 552 | : commutative_ring< T >, 553 | dividable< T > 554 | { 555 | }; 556 | 557 | template< typename T, typename U = T > 558 | class TAO_OPERATORS_BROKEN_EBO ordered_field 559 | : field< T, U >, 560 | totally_ordered< T, U > 561 | { 562 | }; 563 | 564 | TAO_OPERATORS_BASIC_OP( andable, & ); 565 | TAO_OPERATORS_BASIC_OP_LEFT( andable, & ); 566 | TAO_OPERATORS_BASIC_OP_COMMUTATIVE( andable, & ); 567 | 568 | TAO_OPERATORS_BASIC_OP( orable, | ); 569 | TAO_OPERATORS_BASIC_OP_LEFT( orable, | ); 570 | TAO_OPERATORS_BASIC_OP_COMMUTATIVE( orable, | ); 571 | 572 | TAO_OPERATORS_BASIC_OP( xorable, ^); 573 | TAO_OPERATORS_BASIC_OP_LEFT( xorable, ^); 574 | TAO_OPERATORS_BASIC_OP_COMMUTATIVE( xorable, ^); 575 | 576 | template< typename T, typename U = T > 577 | class TAO_OPERATORS_BROKEN_EBO bitwise 578 | : andable< T, U >, 579 | orable< T, U >, 580 | xorable< T, U > 581 | { 582 | }; 583 | 584 | template< typename T, typename U > 585 | class TAO_OPERATORS_BROKEN_EBO bitwise_left 586 | : andable_left< T, U >, 587 | orable_left< T, U >, 588 | xorable_left< T, U > 589 | { 590 | }; 591 | 592 | template< typename T, typename U = T > 593 | class TAO_OPERATORS_BROKEN_EBO commutative_bitwise 594 | : commutative_andable< T, U >, 595 | commutative_orable< T, U >, 596 | commutative_xorable< T, U > 597 | { 598 | }; 599 | 600 | TAO_OPERATORS_BASIC_OP( left_shiftable, << ); 601 | TAO_OPERATORS_BASIC_OP( right_shiftable, >> ); 602 | 603 | template< typename T, typename U = T > 604 | class TAO_OPERATORS_BROKEN_EBO shiftable 605 | : left_shiftable< T, U >, 606 | right_shiftable< T, U > 607 | { 608 | }; 609 | 610 | template< typename T > 611 | class TAO_OPERATORS_BROKEN_EBO incrementable 612 | { 613 | TAO_OPERATORS_NODISCARD friend T operator++( T& arg, int /*unused*/ ) noexcept( noexcept( T( arg ), ++arg, T( std::declval< T >() ) ) ) // NOLINT 614 | { 615 | const T nrv( arg ); 616 | ++arg; 617 | return nrv; 618 | } 619 | }; 620 | 621 | template< typename T > 622 | class TAO_OPERATORS_BROKEN_EBO decrementable 623 | { 624 | TAO_OPERATORS_NODISCARD friend T operator--( T& arg, int /*unused*/ ) noexcept( noexcept( T( arg ), --arg, T( std::declval< T >() ) ) ) // NOLINT 625 | { 626 | const T nrv( arg ); 627 | --arg; 628 | return nrv; 629 | } 630 | }; 631 | 632 | template< typename T > 633 | class TAO_OPERATORS_BROKEN_EBO unit_steppable 634 | : incrementable< T >, 635 | decrementable< T > 636 | { 637 | }; 638 | 639 | } // namespace operators 640 | 641 | } // namespace tao 642 | 643 | #undef TAO_OPERATORS_BASIC_OP 644 | #undef TAO_OPERATORS_BASIC_OP_LEFT 645 | #undef TAO_OPERATORS_BASIC_OP_COMMUTATIVE 646 | 647 | #endif 648 | -------------------------------------------------------------------------------- /src/test/operators/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.3.0 FATAL_ERROR) 2 | 3 | set(test_sources 4 | no_rvalue_reference_results.cpp 5 | test_operators.cpp 6 | ) 7 | 8 | # file(GLOB ...) is used to validate the above list of test_sources 9 | file(GLOB glob_test_sources RELATIVE ${CMAKE_CURRENT_LIST_DIR} *.cpp) 10 | 11 | foreach(testsourcefile ${test_sources}) 12 | if(${testsourcefile} IN_LIST glob_test_sources) 13 | list(REMOVE_ITEM glob_test_sources ${testsourcefile}) 14 | else() 15 | message(SEND_ERROR "File ${testsourcefile} is missing from src/test/operators") 16 | endif() 17 | 18 | get_filename_component(exename ${testsourcefile} NAME_WE) 19 | set(exename "tao-operators-test-${exename}") 20 | add_executable(${exename} ${testsourcefile}) 21 | target_link_libraries(${exename} PRIVATE taocpp::operators) 22 | set_target_properties(${exename} PROPERTIES 23 | CXX_STANDARD 11 24 | CXX_STANDARD_REQUIRED ON 25 | CXX_EXTENSIONS OFF 26 | ) 27 | if(MSVC) 28 | target_compile_options(${exename} PRIVATE /W4 /WX /utf-8) 29 | else() 30 | target_compile_options(${exename} PRIVATE -pedantic -Wall -Wextra -Wshadow -Werror) 31 | endif() 32 | add_test(NAME ${exename} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../.. COMMAND ${exename}) 33 | endforeach(testsourcefile) 34 | 35 | if(glob_test_sources) 36 | foreach(ignored_source_file ${glob_test_sources}) 37 | message(SEND_ERROR "File ${ignored_source_file} in src/test/operators is ignored") 38 | endforeach(ignored_source_file) 39 | endif() 40 | -------------------------------------------------------------------------------- /src/test/operators/no_rvalue_reference_results.cpp: -------------------------------------------------------------------------------- 1 | // The Art of C++ / Operators 2 | // Copyright (c) 2013-2020 Daniel Frey 3 | // Please see LICENSE for license or visit https://github.com/taocpp/operators/ 4 | 5 | #define TAO_OPERATORS_NO_RVALUE_REFERENCE_RESULTS 6 | #include "test_operators.cpp" 7 | -------------------------------------------------------------------------------- /src/test/operators/test_operators.cpp: -------------------------------------------------------------------------------- 1 | // The Art of C++ / Operators 2 | // Copyright (c) 2013-2020 Daniel Frey 3 | // Please see LICENSE for license or visit https://github.com/taocpp/operators/ 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | class X 11 | : tao::operators::ordered_field< X >, 12 | tao::operators::modable< X >, 13 | tao::operators::ordered_field< X, int >, 14 | tao::operators::modable< X, int > 15 | { 16 | public: 17 | explicit X( const int v ) noexcept 18 | : v_( v ) 19 | { 20 | } 21 | 22 | X( const X& ) = default; 23 | X( X&& ) = default; 24 | 25 | ~X() = default; 26 | 27 | X& operator=( const X& ) = delete; 28 | X& operator=( X&& ) = delete; 29 | 30 | X& operator+=( const X& x ) noexcept 31 | { 32 | v_ += x.v_; 33 | return *this; 34 | } 35 | 36 | X& operator-=( const X& x ) 37 | { 38 | v_ -= x.v_; 39 | return *this; 40 | } 41 | 42 | X& operator*=( const X& x ) 43 | { 44 | v_ *= x.v_; 45 | return *this; 46 | } 47 | 48 | X& operator/=( const X& x ) 49 | { 50 | v_ /= x.v_; 51 | return *this; 52 | } 53 | 54 | X& operator%=( const X& x ) 55 | { 56 | v_ %= x.v_; 57 | return *this; 58 | } 59 | 60 | X& operator+=( const int v ) 61 | { 62 | v_ += v; 63 | return *this; 64 | } 65 | 66 | X& operator-=( const int v ) 67 | { 68 | v_ -= v; 69 | return *this; 70 | } 71 | 72 | X& operator*=( const int v ) 73 | { 74 | v_ *= v; 75 | return *this; 76 | } 77 | 78 | X& operator/=( const int v ) 79 | { 80 | v_ /= v; 81 | return *this; 82 | } 83 | 84 | X& operator%=( const int v ) 85 | { 86 | v_ %= v; 87 | return *this; 88 | } 89 | 90 | int v_; // NOLINT 91 | }; 92 | 93 | bool operator==( const X& lhs, const X& rhs ) 94 | { 95 | return lhs.v_ == rhs.v_; 96 | } 97 | 98 | bool operator<( const X& lhs, const X& rhs ) 99 | { 100 | return lhs.v_ < rhs.v_; 101 | } 102 | 103 | bool operator==( const X& x, const int v ) 104 | { 105 | return x.v_ == v; 106 | } 107 | 108 | bool operator<( const X& x, const int v ) 109 | { 110 | return x.v_ < v; 111 | } 112 | 113 | bool operator>( const X& x, const int v ) 114 | { 115 | return x.v_ > v; 116 | } 117 | 118 | class E 119 | : tao::operators::ordered_field< E > 120 | { 121 | }; 122 | 123 | void adl_test( const E& /*unused*/ ) {} 124 | 125 | namespace tao 126 | { 127 | void adl_test( const E& ); 128 | 129 | } // namespace tao 130 | 131 | struct S 132 | : tao::operators::addable< S > 133 | { 134 | S() = default; 135 | 136 | S( const S& a, const S& b ) 137 | : S( a + b ) 138 | { 139 | } 140 | 141 | S& operator+=( const S& /*unused*/ ) noexcept 142 | { 143 | return *this; 144 | } 145 | }; 146 | 147 | #if !defined( _MSC_VER ) || defined( __clang__ ) 148 | 149 | struct C 150 | : tao::operators::less_than_comparable< C >, 151 | tao::operators::less_than_comparable< C, int > 152 | { 153 | const int i_; // NOLINT 154 | 155 | explicit constexpr C( int i ) noexcept 156 | : i_( i ) 157 | { 158 | } 159 | }; 160 | 161 | constexpr bool operator<( const C& lhs, const C& rhs ) noexcept 162 | { 163 | return lhs.i_ < rhs.i_; 164 | } 165 | 166 | constexpr bool operator<( const C& lhs, const int rhs ) noexcept 167 | { 168 | return lhs.i_ < rhs; 169 | } 170 | 171 | constexpr bool operator>( const C& lhs, const int rhs ) noexcept 172 | { 173 | return lhs.i_ > rhs; 174 | } 175 | 176 | #endif 177 | 178 | int main() 179 | { 180 | X x1( 1 ); 181 | X x2( 2 ); 182 | X x3( 3 ); 183 | 184 | static_assert( noexcept( x1 + x2 ), "oops" ); 185 | static_assert( !noexcept( x1 * x2 ), "oops" ); 186 | 187 | assert( x1 == x1 ); 188 | assert( x1 != x2 ); 189 | 190 | assert( x1 == 1 ); 191 | assert( 2 == x2 ); 192 | assert( x3 != 1 ); 193 | assert( 2 != x3 ); 194 | 195 | assert( x1 < x2 ); 196 | assert( x1 <= x2 ); 197 | assert( x2 <= x2 ); 198 | assert( x3 > x2 ); 199 | assert( x3 >= x2 ); 200 | assert( x2 >= x2 ); 201 | 202 | assert( x1 < 2 ); 203 | assert( x1 <= 2 ); 204 | assert( x2 <= 2 ); 205 | assert( x3 > 2 ); 206 | assert( x3 >= 2 ); 207 | assert( x2 >= 2 ); 208 | 209 | assert( 1 < x2 ); 210 | assert( 1 <= x2 ); 211 | assert( 2 <= x2 ); 212 | assert( 3 > x2 ); 213 | assert( 3 >= x2 ); 214 | assert( 2 >= x2 ); 215 | 216 | assert( x1 + x2 == x3 ); 217 | assert( 1 + x2 == x3 ); 218 | assert( x1 + 2 == x3 ); 219 | assert( x2 + x1 == 3 ); 220 | 221 | assert( x3 - x1 == x2 ); 222 | assert( 3 - x1 == x2 ); 223 | assert( x3 - 1 == x2 ); 224 | assert( x1 - x3 == -2 ); 225 | 226 | assert( x2 * x2 == 4 ); 227 | assert( x2 * 3 == 6 ); 228 | assert( 4 * x2 == 8 ); 229 | 230 | assert( ( x3 + x1 ) / x2 == 2 ); 231 | assert( ( x1 + x3 ) / 2 == x2 ); 232 | 233 | assert( x3 % x2 == 1 ); 234 | assert( x3 % 2 == 1 ); 235 | 236 | static_assert( std::is_empty< E >::value, "oops" ); 237 | 238 | adl_test( E{} ); 239 | 240 | S s; 241 | S s2( s, s ); 242 | 243 | #if !defined( _MSC_VER ) || defined( __clang__ ) 244 | 245 | constexpr C c1( 1 ); 246 | constexpr C c2( 2 ); 247 | constexpr C c3( 3 ); 248 | 249 | static_assert( c1 < c2, "oops" ); 250 | static_assert( c2 < c3, "oops" ); 251 | static_assert( c1 < c3, "oops" ); 252 | 253 | static_assert( c1 < 2, "oops" ); 254 | static_assert( c2 < 3, "oops" ); 255 | static_assert( c1 < 3, "oops" ); 256 | 257 | static_assert( c1 <= c2, "oops" ); 258 | static_assert( c2 <= c3, "oops" ); 259 | static_assert( c1 <= c3, "oops" ); 260 | 261 | static_assert( c1 <= 2, "oops" ); 262 | static_assert( c2 <= 3, "oops" ); 263 | static_assert( c1 <= 3, "oops" ); 264 | 265 | static_assert( 1 <= c2, "oops" ); 266 | static_assert( 2 <= c3, "oops" ); 267 | static_assert( 1 <= c3, "oops" ); 268 | 269 | static_assert( c2 > c1, "oops" ); 270 | static_assert( c3 > c2, "oops" ); 271 | static_assert( c3 > c1, "oops" ); 272 | 273 | static_assert( c2 > 1, "oops" ); 274 | static_assert( c3 > 2, "oops" ); 275 | static_assert( c3 > 1, "oops" ); 276 | 277 | static_assert( 2 > c1, "oops" ); 278 | static_assert( 3 > c2, "oops" ); 279 | static_assert( 3 > c1, "oops" ); 280 | 281 | static_assert( c2 >= c1, "oops" ); 282 | static_assert( c3 >= c2, "oops" ); 283 | static_assert( c3 >= c1, "oops" ); 284 | 285 | static_assert( c2 >= 1, "oops" ); 286 | static_assert( c3 >= 2, "oops" ); 287 | static_assert( c3 >= 1, "oops" ); 288 | 289 | static_assert( 2 >= c1, "oops" ); 290 | static_assert( 3 >= c2, "oops" ); 291 | static_assert( 3 >= c1, "oops" ); 292 | 293 | static_assert( !( c1 < c1 ), "oops" ); // NOLINT 294 | static_assert( !( c1 > c1 ), "oops" ); // NOLINT 295 | static_assert( c1 <= c1, "oops" ); // NOLINT 296 | static_assert( c1 >= c1, "oops" ); // NOLINT 297 | 298 | #endif 299 | } 300 | --------------------------------------------------------------------------------