Template Class ModuleHolder¶
Defined in File pimpl.h
Page Contents
Class Documentation¶
-
template<typename Contained>
class ModuleHolder : private torch::detail::ModuleHolderIndicator¶ A
ModuleHolder
is essentially a wrapper aroundstd::shared_ptr<M>
whereM
is annn::Module
subclass, with convenient constructors defined for the kind of constructions we want to allow for our modules.Public Functions
-
inline ModuleHolder()¶
Default constructs the contained module if if has a default constructor, else produces a static error.
NOTE: This uses the behavior of template classes in C++ that constructors (or any methods) are only compiled when actually used.
-
inline ModuleHolder(std::nullptr_t)¶
Constructs the
ModuleHolder
with an empty contained value.Access to the underlying module is not permitted and will throw an exception, until a value is assigned.
-
template<typename Head, typename ...Tail, typename = std::enable_if_t<!(torch::detail::is_module_holder_of<Head, ContainedType>::value && (sizeof...(Tail) == 0))>>
inline explicit ModuleHolder(Head &&head, Tail&&... tail)¶ Constructs the
ModuleHolder
with a contained module, forwarding all arguments to its constructor.
Constructs the
ModuleHolder
from a pointer to the contained type.Example:
Linear(std::make_shared<LinearImpl>(...))
.
-
inline explicit operator bool() const noexcept¶
Returns true if the
ModuleHolder
contains a module, or false if it isnullptr
.
-
inline const std::shared_ptr<Contained> &ptr() const¶
Returns a shared pointer to the underlying module.
-
template<typename ...Args>
inline auto operator()(Args&&... args) -> torch::detail::return_type_of_forward_t<Contained, Args...>¶ Calls the
forward()
method of the contained module.
-
template<typename Arg>
inline decltype(auto) operator[](Arg &&arg)¶ Forwards to the subscript operator of the contained module.
NOTE: std::forward is qualified to prevent VS2017 emitting error C2872: ‘std’: ambiguous symbol
-
inline bool is_empty() const noexcept¶
Returns true if the
ModuleHolder
does not contain a module.
-
inline ModuleHolder()¶