有可能实现这个功能吗?如果是,如何?
是的,这是可能的。最小的变化
á
解决方案没有
std::enable_if
/SFINAE。
// Method to get a Car from the Pack (enabled only if Car is in Types...)
template <typename T = Car>
constexpr auto getCar() -> decltype(std::get<Car>(items_))
{
return std::get<Car>(items_);
}
// Method to get a Person from the Pack (enabled only if Person is in Types...)
template <typename T = Person>
constexpr auto getPerson() -> decltype(std::get<Person>(items_))
{
return std::get<Person>(items_);
}
See live demo
á
请记住,以上内容不会禁止在未评估的上下文中使用该函数(即。
decltype(personPack.getCar()) x = personAndCarPack.getCar();
仍然有效)。
或者,与
std::enable_if_t
和
std::disjunction
,你也可能达到同样的效果。
// Method to get a Car from the Pack (enabled only if Car is in Types...)
template <typename T = Car>
constexpr auto getCar()
-> std::enable_if_t<std::disjunction<std::is_same<Types, T>...>::value, T>
{
static_assert(std::is_same_v<T, Car>, "T must be of type Car!");
return std::get<T>(items_);
}
// Method to get a Person from the Pack (enabled only if Person is in Types...)
template <typename T = Person>
constexpr auto getPerson()
-> std::enable_if_t<std::disjunction<std::is_same<Types, T>...>::value, T>
{
static_assert(std::is_same_v<T, Person>, "T must be of type Person!");
return std::get<T>(items_);
}
See live demo
(面向未来读者)自
c++20
然而,解决方案将更简单
requires
constraints
// Method to get a Car from the Pack (enabled only if Car is in Types...)
constexpr auto getCar() requires (std::is_same_v<Types, Car> || ...)
{
return std::get<Car>(items_);
}
// Method to get a Person from the Pack (enabled only if Person is in Types...)
constexpr auto getPerson() requires (std::is_same_v<Types, Person> || ...)
{
return std::get<Person>(items_);
}
See live demo