├── LICENSE ├── README └── main.c /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, jason-deng 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of C99FunctionOverload nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | # C99FunctionOverload 2 | 3 | Pure standard C99 macro implementation to overload and set optional default parameters of C/C++ functions. Inspired by brilliant people at 4 | 5 | 6 | 7 | Finally I come out with something that incorporates all the tricks, so that the solution 8 | 9 | 1. Uses only **standard C99** macros to achieve function overloading, no GCC/CLANG/MSVC extension involved (i.e., comma swallowing by the specific expression `, ##__VA_ARGS__` for GCC/CLANG, and implicit swallowing by `##__VA_ARGS__` for MSVC). So feel free to pass the missing `--std=c99` to your compiler if you wish =) 10 | 2. Works for **zero argument**, as well as **unlimited number of arguments**, if you expand it further to suit your needs 11 | 3. Works reasonably **cross-platform**, at least tested for 12 | 13 | - **GNU/Linux + GCC** (GCC 4.9.2 on CentOS 7.0 x86_64) 14 | - **GNU/Linux + CLANG/LLVM**, (CLANG/LLVM 3.5.0 on CentOS 7.0 x86_64) 15 | - **OS X + Xcode**, (XCode 6.1.1 on OS X Yosemite 10.10.1) 16 | - **Windows + Visual Studio**, (Visual Studio 2013 Update 4 on Windows 7 SP1 64 bits) 17 | 18 | Please refer to the link 19 | 20 | 21 | 22 | for a more detailed walk-through of the codes. 23 | 24 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void realCreate(int x, int y) 4 | { 5 | printf("(%d, %d)\n", x, y); 6 | } 7 | 8 | #define CREATE_2(x, y) realCreate(x, y) 9 | #define CREATE_1(x) CREATE_2(x, 0) 10 | #define CREATE_0() CREATE_1(0) 11 | 12 | #define FUNC_CHOOSER(_f1, _f2, _f3, ...) _f3 13 | #define FUNC_RECOMPOSER(argsWithParentheses) FUNC_CHOOSER argsWithParentheses 14 | #define CHOOSE_FROM_ARG_COUNT(...) FUNC_RECOMPOSER((__VA_ARGS__, CREATE_2, CREATE_1, )) 15 | #define NO_ARG_EXPANDER() ,,CREATE_0 16 | #define MACRO_CHOOSER(...) CHOOSE_FROM_ARG_COUNT(NO_ARG_EXPANDER __VA_ARGS__ ()) 17 | #define create(...) MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__) 18 | 19 | int main() 20 | { 21 | create(); 22 | create(10); 23 | create(20, 20); 24 | //create(30, 30, 30); // Compilation error 25 | return 0; 26 | } 27 | --------------------------------------------------------------------------------