Skip to content

Instantly share code, notes, and snippets.

@jeremyong
Last active March 16, 2022 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyong/c6e72d525e297c507fbca142b0eaf248 to your computer and use it in GitHub Desktop.
Save jeremyong/c6e72d525e297c507fbca142b0eaf248 to your computer and use it in GitHub Desktop.
// The MIT License
// Copyright 2022 Jeremy Ong
// 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.
#pragma once
#include <cstddef>
#include <type_traits>
// This minimal header allows you to add structured binding support to any C++ struct or class
// To use it, include this header, then add `constexpr static size_t element_count = N;` to your
// class declaration, where `N` is the number of elements your class or struct desugars to.
//
// Then, implement a template method with signature `template <size_t N> auto get();` which returns
// the corresponding element depending on the value of the template parameter N.
//
// For example usage, see this godbolt: https://godbolt.org/z/dxPhq8TE9
// A minimal usage example is as shown:
//
// #include <StructuredBindingSupport.hpp>
//
// struct vec3f {
// static constexpr size_t element_count = 3;
// template <size_t N> auto get() {
// if constexpr (N == 0) { return x; }
// else if constexpr (N == 1) { return y; }
// else if constexpr (N == 2) { return z; }
// }
// private:
// float x, y, z;
// };
//
// // vec3f can now be destructured as auto [x, y, z] = myVec3f; (auto& and auto&& work also)
// Requires T::element_count and T::get<N>
namespace std {
template <typename> struct tuple_size;
template <size_t, typename> struct tuple_element;
template<typename T> struct tuple_size {
static constexpr int value = T::element_count;
};
template<size_t N, typename T> struct tuple_element
{
static constexpr int unused = T::element_count; // SFINAE needed to prevent hijacking std implementation
using type = decltype(std::declval<T>().template get<N>());
};
}
@jeremyong
Copy link
Author

jeremyong commented Mar 16, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment