Skip to content

Commit

Permalink
Eliminate a C++11 code pattern in pimpl.h (#126069)
Browse files Browse the repository at this point in the history
Test Plan: Sandcastle

Differential Revision: D57224687

Pull Request resolved: #126069
Approved by: https://github.com/Skylion007
  • Loading branch information
r-barnes authored and pytorchmergebot committed May 13, 2024
1 parent b9e7b35 commit c098cd0
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions torch/csrc/api/include/torch/nn/pimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,13 @@ class ModuleHolder : torch::detail::ModuleHolderIndicator {
}

private:
/// In C++17, the two methods below could be written as the following:
/// if constexpr (std::is_default_constructible_v<Contained>) {
/// return std::make_shared<Contained>();
/// } else {
/// return nullptr;
/// }
/// In C++11, we use SFINAE instead of `if constexpr`.

template <
typename T = Contained,
typename = torch::enable_if_t<std::is_default_constructible<T>::value>>
std::shared_ptr<Contained> default_construct() {
return std::make_shared<Contained>();
}

template <typename T = Contained>
torch::disable_if_t<
std::is_default_constructible<T>::value,
std::shared_ptr<Contained>>
default_construct() {
return nullptr;
std::shared_ptr<Contained> default_construct() {
if constexpr (std::is_default_constructible_v<T>) {
return std::make_shared<Contained>();
} else {
return nullptr;
}
}
};

Expand Down

0 comments on commit c098cd0

Please sign in to comment.