Skip to content

Commit

Permalink
Use C++17 type_traits throughout (RobotLocomotion#15157)
Browse files Browse the repository at this point in the history
This tidies up math, perception, and vector_gen.
  • Loading branch information
jwnimmer-tri authored Jun 15, 2021
1 parent 79e43c0 commit 9d6a543
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 47 deletions.
24 changes: 12 additions & 12 deletions math/autodiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,29 @@ typename AutoDiffToValueMatrix<Derived>::type autoDiffToValueMatrix(
* @see DiscardZeroGradient
*/
template <typename Derived>
typename std::enable_if<
!std::is_same<typename Derived::Scalar, double>::value,
typename std::enable_if_t<
!std::is_same_v<typename Derived::Scalar, double>,
Eigen::Matrix<typename Derived::Scalar::Scalar, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime, 0, Derived::MaxRowsAtCompileTime,
Derived::MaxColsAtCompileTime>>::type
Derived::MaxColsAtCompileTime>>
DiscardGradient(const Eigen::MatrixBase<Derived>& auto_diff_matrix) {
return autoDiffToValueMatrix(auto_diff_matrix);
}

/// @see DiscardGradient().
template <typename Derived>
typename std::enable_if<
std::is_same<typename Derived::Scalar, double>::value,
const Eigen::MatrixBase<Derived>&>::type
typename std::enable_if_t<
std::is_same_v<typename Derived::Scalar, double>,
const Eigen::MatrixBase<Derived>&>
DiscardGradient(const Eigen::MatrixBase<Derived>& matrix) {
return matrix;
}

/// @see DiscardGradient().
template <typename _Scalar, int _Dim, int _Mode, int _Options>
typename std::enable_if<
!std::is_same<_Scalar, double>::value,
Eigen::Transform<typename _Scalar::Scalar, _Dim, _Mode, _Options>>::type
typename std::enable_if_t<
!std::is_same_v<_Scalar, double>,
Eigen::Transform<typename _Scalar::Scalar, _Dim, _Mode, _Options>>
DiscardGradient(const Eigen::Transform<_Scalar, _Dim, _Mode, _Options>&
auto_diff_transform) {
return Eigen::Transform<typename _Scalar::Scalar, _Dim, _Mode, _Options>(
Expand All @@ -82,9 +82,9 @@ DiscardGradient(const Eigen::Transform<_Scalar, _Dim, _Mode, _Options>&

/// @see DiscardGradient().
template <typename _Scalar, int _Dim, int _Mode, int _Options>
typename std::enable_if<std::is_same<_Scalar, double>::value,
const Eigen::Transform<_Scalar, _Dim, _Mode,
_Options>&>::type
typename std::enable_if_t<std::is_same_v<_Scalar, double>,
const Eigen::Transform<_Scalar, _Dim, _Mode,
_Options>&>
DiscardGradient(
const Eigen::Transform<_Scalar, _Dim, _Mode, _Options>& transform) {
return transform;
Expand Down
26 changes: 13 additions & 13 deletions math/autodiff_gradient.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ void initializeAutoDiffGivenGradientMatrix(
static_assert(static_cast<int>(ExpectedAutoDiffType::ColsAtCompileTime) ==
static_cast<int>(DerivedAutoDiff::ColsAtCompileTime),
"auto diff matrix has wrong number of columns at compile time");
static_assert(std::is_same<typename DerivedAutoDiff::Scalar,
typename ExpectedAutoDiffType::Scalar>::value,
static_assert(std::is_same_v<typename DerivedAutoDiff::Scalar,
typename ExpectedAutoDiffType::Scalar>,
"wrong auto diff scalar type");

typedef typename Eigen::MatrixBase<DerivedGradient>::Index Index;
Expand Down Expand Up @@ -157,11 +157,11 @@ void gradientMatrixToAutoDiff(
* @see DiscardGradient
*/
template <typename Derived>
typename std::enable_if<
!std::is_same<typename Derived::Scalar, double>::value,
typename std::enable_if_t<
!std::is_same_v<typename Derived::Scalar, double>,
Eigen::Matrix<typename Derived::Scalar::Scalar, Derived::RowsAtCompileTime,
Derived::ColsAtCompileTime, 0, Derived::MaxRowsAtCompileTime,
Derived::MaxColsAtCompileTime>>::type
Derived::MaxColsAtCompileTime>>
DiscardZeroGradient(
const Eigen::MatrixBase<Derived>& auto_diff_matrix,
const typename Eigen::NumTraits<
Expand All @@ -177,8 +177,8 @@ DiscardZeroGradient(

/// @see DiscardZeroGradient().
template <typename Derived>
typename std::enable_if<std::is_same<typename Derived::Scalar, double>::value,
const Eigen::MatrixBase<Derived>&>::type
typename std::enable_if_t<std::is_same_v<typename Derived::Scalar, double>,
const Eigen::MatrixBase<Derived>&>
DiscardZeroGradient(const Eigen::MatrixBase<Derived>& matrix,
double precision = 0.) {
unused(precision);
Expand All @@ -187,9 +187,9 @@ DiscardZeroGradient(const Eigen::MatrixBase<Derived>& matrix,

/// @see DiscardZeroGradient().
template <typename _Scalar, int _Dim, int _Mode, int _Options>
typename std::enable_if<
!std::is_same<_Scalar, double>::value,
Eigen::Transform<typename _Scalar::Scalar, _Dim, _Mode, _Options>>::type
typename std::enable_if_t<
!std::is_same_v<_Scalar, double>,
Eigen::Transform<typename _Scalar::Scalar, _Dim, _Mode, _Options>>
DiscardZeroGradient(
const Eigen::Transform<_Scalar, _Dim, _Mode, _Options>& auto_diff_transform,
const typename Eigen::NumTraits<typename _Scalar::Scalar>::Real& precision =
Expand All @@ -200,9 +200,9 @@ DiscardZeroGradient(

/// @see DiscardZeroGradient().
template <typename _Scalar, int _Dim, int _Mode, int _Options>
typename std::enable_if<
std::is_same<_Scalar, double>::value,
const Eigen::Transform<_Scalar, _Dim, _Mode, _Options>&>::type
typename std::enable_if_t<
std::is_same_v<_Scalar, double>,
const Eigen::Transform<_Scalar, _Dim, _Mode, _Options>&>
DiscardZeroGradient(
const Eigen::Transform<_Scalar, _Dim, _Mode, _Options>& transform,
double precision = 0.) {
Expand Down
2 changes: 1 addition & 1 deletion math/bspline_basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class BsplineBasis final {
#else
// Restrict this method to T = double only; we must mix "Archive" into the
// conditional type for SFINAE to work, so we just check it against void.
std::enable_if_t<std::is_same_v<T, double> && !std::is_void<Archive>::value>
std::enable_if_t<std::is_same_v<T, double> && !std::is_void_v<Archive>>
#endif
Serialize(Archive* a) {
a->Visit(MakeNameValue("order", &order_));
Expand Down
4 changes: 2 additions & 2 deletions math/convert_time_derivative.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ Vector3<typename v_Type::Scalar> ConvertTimeDerivativeToOtherFrame(
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Eigen::MatrixBase<w_AB_Type>, 3);
typedef typename v_Type::Scalar T;
// All input vectors must be templated on the same scalar type.
static_assert(std::is_same<typename DtB_v_Type::Scalar, T>::value,
static_assert(std::is_same_v<typename DtB_v_Type::Scalar, T>,
"DtB_v_E must be templated on the same scalar type as v_E");
static_assert(std::is_same<typename w_AB_Type::Scalar, T>::value,
static_assert(std::is_same_v<typename w_AB_Type::Scalar, T>,
"w_AB_E must be templated on the same scalar type as v_E");
return DtB_v_E + w_AB_E.cross(v_E);
}
Expand Down
2 changes: 1 addition & 1 deletion math/jacobian.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ decltype(auto) jacobian(F &&f, Arg &&x) {
using Eigen::Index;
using Eigen::Matrix;

using ArgNoRef = typename std::remove_reference<Arg>::type;
using ArgNoRef = typename std::remove_reference_t<Arg>;

// Argument scalar type.
using ArgScalar = typename ArgNoRef::Scalar;
Expand Down
6 changes: 3 additions & 3 deletions math/test/gray_code_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ void TestGrayCode(const Eigen::Ref<const Eigen::MatrixXi>& gray_codes) {
auto gray_codes_dynamic = CalculateReflectedGrayCodes(NumDigits);
auto gray_codes_static = CalculateReflectedGrayCodes<NumDigits>();
static_assert(
std::is_same<decltype(gray_codes_dynamic), Eigen::MatrixXi>::value,
std::is_same_v<decltype(gray_codes_dynamic), Eigen::MatrixXi>,
"Should be a dynamic sized matrix");
static_assert(std::is_same<decltype(gray_codes_static),
Eigen::Matrix<int, NumCodes, NumDigits>>::value,
static_assert(std::is_same_v<decltype(gray_codes_static),
Eigen::Matrix<int, NumCodes, NumDigits>>,
"Should be a static sized matrix");
EXPECT_TRUE(CompareMatrices(gray_codes, gray_codes_dynamic, 1E-5,
MatrixCompareType::absolute));
Expand Down
20 changes: 10 additions & 10 deletions math/test/jacobian_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TEST_F(AutodiffJacobianTest, QuadraticForm) {
#pragma GCC diagnostic ignored "-Wshadow"
auto quadratic_form = [&](const auto& x) {
#pragma GCC diagnostic pop
using Scalar = typename std::remove_reference<decltype(x)>::type::Scalar;
using Scalar = typename std::remove_reference_t<decltype(x)>::Scalar;
return (x.transpose() * A.cast<Scalar>().eval() * x).eval();
};

Expand All @@ -50,14 +50,14 @@ TEST_F(AutodiffJacobianTest, QuadraticForm) {
auto jac_chunk_size_6 = jacobian<6>(quadratic_form, x);

// Ensure that chunk size has no effect on output type.
static_assert(std::is_same<decltype(jac_chunk_size_default),
decltype(jac_chunk_size_1)>::value,
static_assert(std::is_same_v<decltype(jac_chunk_size_default),
decltype(jac_chunk_size_1)>,
"jacobian output type mismatch");
static_assert(std::is_same<decltype(jac_chunk_size_default),
decltype(jac_chunk_size_3)>::value,
static_assert(std::is_same_v<decltype(jac_chunk_size_default),
decltype(jac_chunk_size_3)>,
"jacobian output type mismatch");
static_assert(std::is_same<decltype(jac_chunk_size_default),
decltype(jac_chunk_size_6)>::value,
static_assert(std::is_same_v<decltype(jac_chunk_size_default),
decltype(jac_chunk_size_6)>,
"jacobian output type mismatch");

// Ensure that the results are the same.
Expand Down Expand Up @@ -109,7 +109,7 @@ TEST_F(AutoDiffHessianTest, QuadraticFunction) {
#pragma GCC diagnostic ignored "-Wshadow"
auto quadratic_function = [&](const auto& x) {
#pragma GCC diagnostic pop
using Scalar = typename std::remove_reference<decltype(x)>::type::Scalar;
using Scalar = typename std::remove_reference_t<decltype(x)>::Scalar;
return ((A.cast<Scalar>() * x + b.cast<Scalar>()).transpose() *
C.cast<Scalar>() * (D.cast<Scalar>() * x + e.cast<Scalar>()))
.eval();
Expand All @@ -122,8 +122,8 @@ TEST_F(AutoDiffHessianTest, QuadraticFunction) {
auto hess_chunk_size_2_4 = hessian<2, 4>(quadratic_function, x);

// Ensure that chunk size has no effect on output type.
static_assert(std::is_same<decltype(hess_chunk_size_default),
decltype(hess_chunk_size_2_4)>::value,
static_assert(std::is_same_v<decltype(hess_chunk_size_default),
decltype(hess_chunk_size_2_4)>,
"hessian output type mismatch");

// Ensure that the results are the same.
Expand Down
2 changes: 1 addition & 1 deletion perception/test/depth_image_to_point_cloud_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ TYPED_TEST(DepthImageToPointCloudTest, NanValue) {

// This test only applies for 32F images; there is no NaN for 16U images.
constexpr bool is_meaningful_nan =
!std::is_same<TestFixturePixel, uint16_t>::value;
!std::is_same_v<TestFixturePixel, uint16_t>;
if (!is_meaningful_nan) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion perception/test/point_cloud_flags_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ GTEST_TEST(PointCloudFlagsTest, ConstExpr) {
// `constexpr` is attempted with a non-literal-type class (as of C++14).
// TODO(eric.cousineau): Replace with the successor of is_literal_type.
// @ref https://stackoverflow.com/a/40352351/7829525
EXPECT_TRUE(std::is_literal_type<pcf::DescriptorType>::value);
EXPECT_TRUE(std::is_literal_type_v<pcf::DescriptorType>);
}

GTEST_TEST(PointCloudFlagsTest, Formatting) {
Expand Down
2 changes: 1 addition & 1 deletion tools/vector_gen/lcm_vector_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def generate_copy_and_assign(hh, caller_context):
/// Create a symbolic::Variable for each element with the known variable
/// name. This is only available for T == symbolic::Expression.
template <typename U=T>
typename std::enable_if<std::is_same<U, symbolic::Expression>::value>::type
typename std::enable_if_t<std::is_same_v<U, symbolic::Expression>>
SetToNamedVariables() {
"""
SET_TO_NAMED_VARIABLES_BODY = """
Expand Down
2 changes: 1 addition & 1 deletion tools/vector_gen/test/goal/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Sample final : public drake::systems::BasicVector<T> {
/// Create a symbolic::Variable for each element with the known variable
/// name. This is only available for T == symbolic::Expression.
template <typename U = T>
typename std::enable_if<std::is_same<U, symbolic::Expression>::value>::type
typename std::enable_if_t<std::is_same_v<U, symbolic::Expression>>
SetToNamedVariables() {
this->set_x(symbolic::Variable("x"));
this->set_two_word(symbolic::Variable("two_word"));
Expand Down
2 changes: 1 addition & 1 deletion tools/vector_gen/test/sample_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ GTEST_TEST(SampleTest, SimpleCoverage) {
// When inheritance is in use, we should only permit public copy & move
// operations to exist on classes that are marked as final.
static_assert(
std::is_final<Sample<double>>::value,
std::is_final_v<Sample<double>>,
"Sample<T> should have been final");

// Confirm that copy semantics work.
Expand Down

0 comments on commit 9d6a543

Please sign in to comment.