Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
Workshop: Safe and Efficient C++ for Embedded Environments
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
#include <iostream> #include <utility> namespace detail { template<std::size_t> struct index_tag{}; // one type per index used template<class T, size_t N, size_t I> // main template T Xor_helper(const T(&arr)[N], index_tag<I>) { // recurse using `index_tag<I - 1>`: return Xor_helper(arr, index_tag<I - 1>()) ^ arr[I]; } // Recursion stops when `index_tag<0>` is used: template<class T, size_t N> // terminating case T Xor_helper(const T(&arr)[N], index_tag<0>) { return arr[0]; // no recursion, just return the value } } // namespace detail template<class T, size_t N> T Xor(const T(&arr)[N]) { // Call the helper using the index_tag for the last index in the array: return detail::Xor_helper(arr, detail::index_tag<N - 1>()); } int main() { int arr[] = {0xF, 0x1, 0x3}; std::cout << std::hex << Xor(arr); }
Insight:
Console: