Ad
How To Use Concepts In If Statement
I have a concept which checks whether a type is iterable or not
template<typename T>
concept Iterable = requires(T t) {
t.begin();
};
I cannot use it in a template due to problems with overloading, so I'd like to do something similar to the following:
template<typename T>
void universal_function(T x) {
if (x is Iterable)
// something which works with iterables
else if (x is Printable)
// another thing
else
// third thing
}
Ad
Answer
Concept instantiations implicitely convert to boolean values, so they can be used in if
statements. You will need to use if constexpr
to achieve the desired behavior, as it will allow for branches containing code that would be invalid in a different branch:
if constexpr (Iterable<T>) {
// ...
} else if constexpr (Printable<T>) {
// ...
} else {
// ...
}
Ad
source: stackoverflow.com
Related Questions
- → Comparing two large files are taking over four hours
- → Setting JSON node name to variable value
- → Compiling GLUT using Emscripten
- → Evaluate check box from a scanned image in node.js
- → Find an easy web server framework for mobile game
- → my https C++ code doesn't work on some sites (binance)
- → Error while opening pivx wallet on ubuntu
- → Why sending a POST by AJAX is interpreted by the HTTP Server as OPTIONS and sending by CURL is effectively a PUT?
- → Python reading in one line multiple types for a calculator
- → How do I properly pass an argument to a function
- → Accessing Websql database with Qt
- → Using Mysql C API for c++ codes
- → How do I set constants at run-time in a c++ header file, imported through Cython?
Ad