From ef8c3caf1e3804e1626fbaacc5f67ffb58c12f57 Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Thu, 9 Nov 2023 16:17:46 -0600 Subject: [PATCH 1/3] Add ValueAlias. --- src/type_traits/complex_help.hpp | 19 +++++++++ src/type_traits/tests/CMakeLists.txt | 2 +- src/type_traits/tests/test_complex_helper.cpp | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/type_traits/tests/test_complex_helper.cpp diff --git a/src/type_traits/complex_help.hpp b/src/type_traits/complex_help.hpp index 79e0e920a4..fbce9a633f 100644 --- a/src/type_traits/complex_help.hpp +++ b/src/type_traits/complex_help.hpp @@ -12,6 +12,9 @@ #ifndef QMCPLUSPLUS_COMPLEX_HELP_HPP #define QMCPLUSPLUS_COMPLEX_HELP_HPP +#include +#include + namespace qmcplusplus { template @@ -44,6 +47,22 @@ struct RealAlias_impl> { using value_type = typename T::value_ty template using RealAlias = typename RealAlias_impl::value_type; +template +struct ValueAlias_impl {}; + +template +struct ValueAlias_impl> { using value_type = TREAL; }; + +template +struct ValueAlias_impl> { using value_type = std::complex; }; + +/** If you need to make a value type of a given precision based on a reference value type + * set the desired POD float point type as TREAL and set the reference type as TREF. + * If TREF is real/complex, the generated Value type is real/complex. + */ +template ::value>> +using ValueAlias = typename ValueAlias_impl::value_type; + ///real part of a scalar. Cannot be replaced by std::real due to AFQMC specific needs. inline float real(const float& c) { return c; } inline double real(const double& c) { return c; } diff --git a/src/type_traits/tests/CMakeLists.txt b/src/type_traits/tests/CMakeLists.txt index 5761a14c90..08bf12935c 100644 --- a/src/type_traits/tests/CMakeLists.txt +++ b/src/type_traits/tests/CMakeLists.txt @@ -14,7 +14,7 @@ set(SRC_DIR type_traits) set(UTEST_EXE test_${SRC_DIR}) set(UTEST_NAME deterministic-unit_test_${SRC_DIR}) -set(TEST_SRCS test_qmctypes.cpp test_template_types.cpp) +set(TEST_SRCS test_qmctypes.cpp test_template_types.cpp test_complex_helper.cpp) add_executable(${UTEST_EXE} ${TEST_SRCS}) target_link_libraries(${UTEST_EXE} catch_main containers) diff --git a/src/type_traits/tests/test_complex_helper.cpp b/src/type_traits/tests/test_complex_helper.cpp new file mode 100644 index 0000000000..da193cf82d --- /dev/null +++ b/src/type_traits/tests/test_complex_helper.cpp @@ -0,0 +1,40 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2023 QMCPACK developers +// +// File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory +// +// File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory +////////////////////////////////////////////////////////////////////////////////////// + +#include "catch.hpp" +#include "type_traits/complex_help.hpp" + +namespace qmcplusplus +{ +template +class TestComplexHelper +{ + using Cmplx = std::complex

; + using Real = RealAlias; + using CmplxRebuild = ValueAlias; +public: + void run() + { + Cmplx aa; + CmplxRebuild bb; + aa = bb; + } +}; + +TEST_CASE("complex_helper", "[type_traits]") +{ + TestComplexHelper float_test; + float_test.run(); + TestComplexHelper double_test; + double_test.run(); +} + +} // namespace qmcplusplus From fbb07f4ba0ce411aec8a69f6caaed50197276d1d Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Thu, 9 Nov 2023 16:20:56 -0600 Subject: [PATCH 2/3] Formatting. --- src/type_traits/complex_help.hpp | 44 ++++++++++++------- src/type_traits/tests/test_complex_helper.cpp | 5 ++- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/type_traits/complex_help.hpp b/src/type_traits/complex_help.hpp index fbce9a633f..6a309084a9 100644 --- a/src/type_traits/complex_help.hpp +++ b/src/type_traits/complex_help.hpp @@ -29,14 +29,21 @@ using IsComplex = std::enable_if_t::value, bool>; template using IsReal = std::enable_if_t::value, bool>; -template -struct RealAlias_impl {}; +template +struct RealAlias_impl +{}; -template -struct RealAlias_impl> { using value_type = T; }; +template +struct RealAlias_impl> +{ + using value_type = T; +}; -template -struct RealAlias_impl> { using value_type = typename T::value_type; }; +template +struct RealAlias_impl> +{ + using value_type = typename T::value_type; +}; /** If you have a function templated on a value that can be real or complex * and you need to get the base Real type if its complex or just the real. @@ -44,23 +51,30 @@ struct RealAlias_impl> { using value_type = typename T::value_ty * If you try to do this on anything but a fp or a std::complex you will * get a compilation error. */ -template +template using RealAlias = typename RealAlias_impl::value_type; -template -struct ValueAlias_impl {}; +template +struct ValueAlias_impl +{}; -template -struct ValueAlias_impl> { using value_type = TREAL; }; +template +struct ValueAlias_impl> +{ + using value_type = TREAL; +}; -template -struct ValueAlias_impl> { using value_type = std::complex; }; +template +struct ValueAlias_impl> +{ + using value_type = std::complex; +}; /** If you need to make a value type of a given precision based on a reference value type * set the desired POD float point type as TREAL and set the reference type as TREF. * If TREF is real/complex, the generated Value type is real/complex. */ -template ::value>> +template::value>> using ValueAlias = typename ValueAlias_impl::value_type; ///real part of a scalar. Cannot be replaced by std::real due to AFQMC specific needs. @@ -78,7 +92,7 @@ inline float conj(const float& c) { return c; } inline double conj(const double& c) { return c; } inline std::complex conj(const std::complex& c) { return std::conj(c); } inline std::complex conj(const std::complex& c) { return std::conj(c); } - + } // namespace qmcplusplus #endif diff --git a/src/type_traits/tests/test_complex_helper.cpp b/src/type_traits/tests/test_complex_helper.cpp index da193cf82d..9333ed2dd4 100644 --- a/src/type_traits/tests/test_complex_helper.cpp +++ b/src/type_traits/tests/test_complex_helper.cpp @@ -17,9 +17,10 @@ namespace qmcplusplus template class TestComplexHelper { - using Cmplx = std::complex

; - using Real = RealAlias; + using Cmplx = std::complex

; + using Real = RealAlias; using CmplxRebuild = ValueAlias; + public: void run() { From 1776046f09579565d4be2dd6e780bd5a0dd17d54 Mon Sep 17 00:00:00 2001 From: Ye Luo Date: Thu, 9 Nov 2023 16:23:30 -0600 Subject: [PATCH 3/3] Add a test for Real case. --- src/type_traits/tests/test_complex_helper.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/type_traits/tests/test_complex_helper.cpp b/src/type_traits/tests/test_complex_helper.cpp index 9333ed2dd4..b93b0717fa 100644 --- a/src/type_traits/tests/test_complex_helper.cpp +++ b/src/type_traits/tests/test_complex_helper.cpp @@ -20,6 +20,7 @@ class TestComplexHelper using Cmplx = std::complex

; using Real = RealAlias; using CmplxRebuild = ValueAlias; + using RealRebuild = ValueAlias; public: void run() @@ -27,6 +28,10 @@ class TestComplexHelper Cmplx aa; CmplxRebuild bb; aa = bb; + + Real cc; + RealRebuild dd(0); + cc = dd; } };