Functional.h#

Fully qualified name: carb/cpp/Functional.h

File members: carb/cpp/Functional.h

// SPDX-FileCopyrightText: Copyright (c) 2020-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: LicenseRef-NvidiaProprietary
//
// NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
// property and proprietary rights in and to this material, related
// documentation and any modifications thereto. Any use, reproduction,
// disclosure or distribution of this material and related documentation
// without an express license agreement from NVIDIA CORPORATION or
// its affiliates is strictly prohibited.

#pragma once

#include "TypeTraits.h"
#include "detail/ImplInvoke.h"
#include "../detail/NoexceptType.h"

#include <type_traits>

namespace carb
{
namespace cpp
{

CARB_DETAIL_PUSH_IGNORE_NOEXCEPT_TYPE()

template <typename Func, typename... TArgs>
constexpr invoke_result_t<Func, TArgs...> invoke(Func&& f,
                                                 TArgs&&... args) noexcept(is_nothrow_invocable<Func, TArgs...>::value)
{
    return detail::invoke_impl<std::decay_t<Func>>::eval(std::forward<Func>(f), std::forward<TArgs>(args)...);
}

namespace detail
{

// This is needed to handle calling a function which returns a non-void when `R` is void. The only difference is the
// lack of a return statement.
template <typename R, typename Func, typename... TArgs>
constexpr std::enable_if_t<std::is_void<R>::value> invoke_r_impl(Func&& f, TArgs&&... args) noexcept(
    is_nothrow_invocable_r<R, Func, TArgs...>::value)
{
    detail::invoke_impl<std::decay_t<Func>>::eval(std::forward<Func>(f), std::forward<TArgs>(args)...);
}

template <typename R, typename Func, typename... TArgs>
constexpr std::enable_if_t<!std::is_void<R>::value, R> invoke_r_impl(Func&& f, TArgs&&... args) noexcept(
    is_nothrow_invocable_r<R, Func, TArgs...>::value)
{
    return detail::invoke_impl<std::decay_t<Func>>::eval(std::forward<Func>(f), std::forward<TArgs>(args)...);
}

} // namespace detail

template <typename R, typename Func, typename... TArgs>
constexpr R invoke_r(Func&& f, TArgs&&... args) noexcept(is_nothrow_invocable_r<R, Func, TArgs...>::value)
{
    return detail::invoke_r_impl<R>(std::forward<Func>(f), std::forward<TArgs>(args)...);
}

CARB_DETAIL_POP_IGNORE_NOEXCEPT_TYPE()

} // namespace cpp
} // namespace carb