I want to try the use of structured binding(tuple-like by add std::tuple_size and std::tuple_element) in C++ for “FVectorxd”, but I keep encountering errors when trying.
However, when I tried to replicate it simply on the “Compiler Explorer” website, the compilation was successful . So I’m not sure where the issue lies.
Intellisense does not show error prompts.
#pragma once
#include <utility>
#include <type_traits>
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Tools.generated.h"
// mathch vectorxd concepts
template<typename T>
concept contain_freal_type = requires
{
typename T::FReal;
};
template<typename Vector>
concept vector_2d_type = contain_freal_type<Vector> && requires
{
Vector::X;
Vector::Y;
Vector::XY;
};
template<typename Vector>
concept vector_3d_type = contain_freal_type<Vector> && requires
{
Vector::X;
Vector::Y;
Vector::Z;
Vector::XYZ;
};
template<typename Vector>
concept vector_4d_type = contain_freal_type<Vector> && requires
{
Vector::X;
Vector::Y;
Vector::Z;
Vector::W;
Vector::XYZW;
};
template<typename Vector>
concept vector_type = vector_2d_type<Vector> || vector_3d_type<Vector>
|| vector_4d_type<Vector>;
// add std::tuple_size and std::tuple_element
namespace std
{
template<::vector_type Vector2D>
struct tuple_size<Vector2D> : std::integral_constant<size_t, 2>
{};
template<::vector_3d_type Vector3D>
struct tuple_size<Vector3D> : std::integral_constant<size_t, 3>
{};
template<::vector_4d_type Vector4D>
struct tuple_size<Vector4D> : std::integral_constant<size_t, 4>
{};
template<::vector_type Vector, size_t Index>
struct tuple_element<Index, Vector>
{
using type = typename Vector::FReal;
};
} // end namespace std
template<size_t Index>
constexpr auto Get(::vector_type auto const &Vec)
{
if constexpr (Index == 0) return Vec.X;
if constexpr (Index == 1) return Vec.Y;
if constexpr (Index == 2) return Vec.Z;
if constexpr (Index == 3) return Vec.W;
}
template<size_t Index>
auto get(::vector_type auto &Vec)
{
return Get<Index>(Vec);
}
template<size_t Index>
auto get(::vector_type auto const &Vec)
{
return Get<Index>(Vec);
}
template<size_t Index>
auto get(::vector_type auto &&Vec)
{
return Get<Index>(Vec);
}
template<size_t Index>
auto get(::vector_type auto const &&Vec)
{
return Get<Index>(Vec);
}
UCLASS()
class DEMO_API UTools : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
// simply test
static void Test()
{
auto[X, Y, Z] = FVector(1., 2., 3.0);
}
};
I am using the “MSVC-14.34.31933” compilation environment with the C++ standard set to “CppStandardVersion.Latest”.
IDE: Rider;
Thank OGs!