From 8a12a97ce6350e739cb1963f6a2f9be210dde303 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Sun, 3 Dec 2023 16:46:04 -0800 Subject: [PATCH] Make wpi::ct_string implicitly convert to std::string --- wpilibc/src/test/native/cpp/util/Color8BitTest.cpp | 5 +++++ wpilibc/src/test/native/cpp/util/ColorTest.cpp | 5 +++++ wpiutil/src/main/native/include/wpi/ct_string.h | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/wpilibc/src/test/native/cpp/util/Color8BitTest.cpp b/wpilibc/src/test/native/cpp/util/Color8BitTest.cpp index 733c43a3dc6..d9027be2445 100644 --- a/wpilibc/src/test/native/cpp/util/Color8BitTest.cpp +++ b/wpilibc/src/test/native/cpp/util/Color8BitTest.cpp @@ -2,6 +2,8 @@ // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. +#include + #include #include "frc/util/Color8Bit.h" @@ -59,6 +61,9 @@ TEST(Color8BitTest, ToHexString) { constexpr frc::Color8Bit color1{255, 128, 64}; EXPECT_EQ("#FF8040", color1.HexString()); + // Ensure conversion to std::string works + [[maybe_unused]] std::string str = color1.HexString(); + frc::Color8Bit color2{255, 128, 64}; EXPECT_EQ("#FF8040", color2.HexString()); } diff --git a/wpilibc/src/test/native/cpp/util/ColorTest.cpp b/wpilibc/src/test/native/cpp/util/ColorTest.cpp index 6722e858ca5..8b71d4634c1 100644 --- a/wpilibc/src/test/native/cpp/util/ColorTest.cpp +++ b/wpilibc/src/test/native/cpp/util/ColorTest.cpp @@ -2,6 +2,8 @@ // Open Source Software; you can modify and/or share it under the terms of // the WPILib BSD license file in the root directory of this project. +#include + #include #include "frc/util/Color.h" @@ -59,6 +61,9 @@ TEST(ColorTest, ToHexString) { constexpr frc::Color color1{255, 128, 64}; EXPECT_EQ("#FF8040", color1.HexString()); + // Ensure conversion to std::string works + [[maybe_unused]] std::string str = color1.HexString(); + frc::Color color2{255, 128, 64}; EXPECT_EQ("#FF8040", color2.HexString()); } diff --git a/wpiutil/src/main/native/include/wpi/ct_string.h b/wpiutil/src/main/native/include/wpi/ct_string.h index e7c52087faf..f85e71931d3 100644 --- a/wpiutil/src/main/native/include/wpi/ct_string.h +++ b/wpiutil/src/main/native/include/wpi/ct_string.h @@ -99,6 +99,11 @@ struct ct_string { constexpr auto data() const noexcept { return chars.data(); } constexpr auto c_str() const noexcept { return chars.data(); } + constexpr operator std::basic_string() // NOLINT + const noexcept { + return std::basic_string{chars.data(), N}; + } + constexpr operator std::basic_string_view() // NOLINT const noexcept { return std::basic_string_view{chars.data(), N};