online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. GS 10 Mar. 2020 *******************************************************************************/ #include <stdint.h> #include <stdio.h> #include <type_traits> // std::is_same() // Templates: https://www.tutorialspoint.com/cplusplus/cpp_templates.htm // Goal: test the inputs to a "C macro" (Templated function in this case in C++) to ensure // they are 1) all the same type, and 2) an unsigned integer type // 1. This template forces all input parameters to be of the *exact same type*, even // though that type isn't fixed to one type! This is because all 4 inputs to test_func() // are of type `T`. template <typename T> void test_func(T a, T b, T c, T d) { printf("test_func: a = %u; b = %u; c = %u; d = %u\n", a, b, c, d); // 2. The 2nd half of the check: // check to see if the type being passed in is uint8_t OR uint16_t OR uint32_t OR uint64_t! static_assert(std::is_same<decltype(a), uint8_t>::value || std::is_same<decltype(a), uint16_t>::value || std::is_same<decltype(a), uint32_t>::value || std::is_same<decltype(a), uint64_t>::value, "This code expects the type to be an unsigned integer type\n" "only (uint8_t, uint16_t, uint32_t, or uint64_t)."); } int main() { printf("Begin\n"); // // TEST A: This FAILS the static assert since they aren't unsigned // int i1 = 10; // test_func(i1, i1, i1, i1); // // TEST B: This FAILS to find a valid function from the template since // // they are all of different types // uint8_t i2 = 11; // uint8_t i3 = 12; // uint32_t i4 = 13; // uint32_t i5 = 14; // test_func(i2, i3, i4, i5); // TEST C: this works! uint16_t i6 = 15; uint16_t i7 = 16; uint16_t i8 = 17; uint16_t i9 = 18; test_func(i6, i7, i8, i9); return 0; } /* TEST A OUTPUT: main.cpp: In instantiation of ‘void test_func(T, T, T, T) [with T = int]’: <span class="error_line" onclick="ide.gotoLine('main.cpp',46)">main.cpp:46:29</span>: required from here main.cpp:32:5: error: static assertion failed: This code expects the type to be an unsigned integer type only (uint8_t, uint16_t, uint32_t, or uint64_t). static_assert(std::is_same<decltype(a), uint8_t>::value || ^~~~~~~~~~~~~ TEST B OUTPUT: main.cpp: In function ‘int main()’: main.cpp:54:29: error: no matching function for call to ‘test_func(uint8_t&, uint8_t&, uint32_t&, uint32_t&)’ test_func(i2, i3, i4, i5); ^ main.cpp:26:6: note: candidate: template void test_func(T, T, T, T) void test_func(T a, T b, T c, T d) ^~~~~~~~~ main.cpp:26:6: note: template argument deduction/substitution failed: main.cpp:54:29: note: deduced conflicting types for parameter ‘T’ (‘unsigned char’ and ‘unsigned int’) test_func(i2, i3, i4, i5); ^ TEST C OUTPUT: Begin test_func: a = 15; b = 16; c = 17; d = 18 */

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue