C++ Metaprogramming Utility
A simple single-file implementation of a metaprogramming utility for function type deduction. FunctionType is a template utility struct implemented using explicit specialization. ISO C++14, ISO C++17 and ISO C++20 compatible.
This is a single-include library. No additional dependencies are required.
FunctionType is able to deduce the decayed function's type, arguments and return value for most function types (including lambdas). FunctionType supports the following qualifiers, and their permitted permutations:
- const qualifier
- volatile qualifier
- mutable specifier
- reference qualifier
- rvalue reference qualifier
- noexcept qualifier (since ISO C++17)
Due to the changes to type deduction in ISO C++17, mainly due to the P0012R1 Exception specification as part of the type system feature, ISO C++17 is the current default minimum supported version. If you would like to use the utility with ISO C++14, define FUNCTION_TYPE_CPP14 before you include the FunctionType.h header. This workaround is needed due to MSC not initializing __cplusplus variable properly.
Copy the FunctionType.h header to your project source files, or include directly from cloned path.
// Usage:
// FunctionType<[function type]>
// Example:
#include "FunctionType.h"
#include <functional>
int StaticFunction(int, float) { return 1; }
int main()
{
FunctionType<decltype(&StaticFunction)>::ReturnType var = 0;
std::function<FunctionType<decltype(&StaticFunction)>::Type> f = StaticFunction;
return 0;
}
Tests.cpp has the full test suite, please refer to it for additional examples.
The following language limitations apply:
- Cannot be used with C-style variadic functions, as we cannot deduce the function argument types from ellipsis.
- Cannot be used with generic lambdas, as we cannot deduce the auto type at declaration.
- Cannot be used with class function overloads, as we cannot deduce the function overload from name alone (Note: it is possible to deduce the overload via a static cast to exact type).
Tested with:
- g++ 7.4.0
- clang 6.0.0
- MSC 1920
Compiled with ISO C++14, ISO C++17, and ISO C++20 flags.
The MIT License (MIT)
Copyright (c) 2023 Konstantin Udovickij
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.