创建一个有区别的联合 / 标记的变体我得出结论,在这样的特性中特别需要"在编译时使某些条件下的析构函数变得微不足道&#34 34 ;.我的意思是某种SFINAE或类似的东西(伪代码):
template< typename ...types >
struct X
{
~X() = default((std::is_trivially_destructible< types >{} && ...))
{
// non-trivial code here
}
};
这意味着如果default(*)
中的条件为true
,则析构函数的定义等于~X() = default;
,但如果它是false
则{ // ... }
正文反而用了。
#pragma once
#include <type_traits>
#include <utility>
#include <experimental/optional>
#include <cassert>
template< typename ...types >
class U;
template<>
class U<>
{
U() = delete;
U(U &) = delete;
U(U const &) = delete;
U(U &&) = delete;
U(U const &&) = delete;
void operator = (U &) = delete;
void operator = (U const &) = delete;
void operator = (U &&) = delete;
void operator = (U const &&) = delete;
};
template< typename first, typename ...rest >
class U< first, rest... >
{
struct head
{
std::size_t which_;
first value_;
template< typename ...types >
constexpr
head(std::experimental::in_place_t, types &&... _values)
: which_{sizeof...(rest)}
, value_(std::forward< types >(_values)...)
{ ; }
template< typename type >
constexpr
head(type && _value)
: head(std::experimental::in_place, std::forward< type >(_value))
{ ; }
};
using tail = U< rest... >;
union
{
head head_;
tail tail_;
};
template< typename ...types >
constexpr
U(std::true_type, types &&... _values)
: head_(std::forward< types >(_values)...)
{ ; }
template< typename ...types >
constexpr
U(std::false_type, types &&... _values)
: tail_(std::forward< types >(_values)...)
{ ; }
public :
using this_type = first; // place for recursive_wrapper filtering
constexpr
std::size_t
which() const
{
return head_.which_;
}
constexpr
U()
: U(typename std::is_default_constructible< this_type >::type{}, std::experimental::in_place)
{ ; }
U(U &) = delete;
U(U const &) = delete;
U(U &&) = delete;
U(U const &&) = delete;
template< typename type >
constexpr
U(type && _value)
: U(typename std::is_same< this_type, std::decay_t< type > >::type{}, std::forward< type >(_value))
{ ; }
template< typename ...types >
constexpr
U(std::experimental::in_place_t, types &&... _values)
: U(typename std::is_constructible< this_type, types... >::type{}, std::experimental::in_place, std::forward< types >(_values)...)
{ ; }
void operator = (U &) = delete;
void operator = (U const &) = delete;
void operator = (U &&) = delete;
void operator = (U const &&) = delete;
template< typename type >
constexpr
void
operator = (type && _value) &
{
operator std::decay_t< type > & () = std::forward< type >(_value);
}
constexpr
explicit
operator this_type & () &
{
assert(sizeof...(rest) == which());
return head_.value_;
}
constexpr
explicit
operator this_type const & () const &
{
assert(sizeof...(rest) == which());
return head_.value_;
}
constexpr
explicit
operator this_type && () &&
{
assert(sizeof...(rest) == which());
return std::move(head_.value_);
}
constexpr
explicit
operator this_type const && () const &&
{
assert(sizeof...(rest) == which());
return std::move(head_.value_);
}
template< typename type >
constexpr
explicit
operator type & () &
{
return static_cast< type & >(tail_);
}
template< typename type >
constexpr
explicit
operator type const & () const &
{
return static_cast< type const & >(tail_);
}
template< typename type >
constexpr
explicit
operator type && () &&
{
//return static_cast< type && >(std::move(tail_)); // There is known clang++ bug #19917 for static_cast to rvalue reference.
return static_cast< type && >(static_cast< type & >(tail_)); // workaround
}
template< typename type >
constexpr
explicit
operator type const && () const &&
{
//return static_cast< type const && >(std::move(tail_));
return static_cast< type const && >(static_cast< type const & >(tail_));
}
~U()
{
if (which() == sizeof...(rest)) {
head_.~head();
} else {
tail_.~tail();
}
}
};
// main.cpp
#include <cstdlib>
int
main()
{
U< int, double > u{1.0};
assert(static_cast< double >(u) == 1.0);
u = 0.0;
assert(static_cast< double >(u) == 0.0);
U< int, double > w{1};
assert(static_cast< int >(w) == 1);
return EXIT_SUCCESS;
}
在此示例中,为了使类U
成为文字类型(在first, rest...
的情况下都是可以轻易破坏的),可以定义与U
类几乎相同({ {1}}),但没有析构函数V
的定义(即如果所有降序类型都是文字,则为文字类型)。然后定义模板类型别名
~U
并重新定义template< typename ...types >
using W = std::conditional_t< (std::is_trivially_destructible< types >{} && ...), V< types... >, U< types... > >;
和using tail = W< rest... >;
中的U
。因此,有两个几乎相同的类,仅在析构函数存在时不同。上述方法需要过多的代码重复。
该问题还涉及简单地复制/移动可分配类型和V
以及类型为operator =
的所有其他条件。 5个条件总共提供了2 ^ 5个组合来实现。
现在 C ++ 中是否有任何现成的技术(并且更简洁,然后在上面描述),我想念,或者很快就会提出建议?
另一个可以想到的方法是(语言特性)将析构函数标记为std::is_trivially_copyable
并授予编译器在实例化期间测试主体是否等同于普通的主体。
更新
在评论中指出简化了代码:constexpr
变为union
- 就像上课一样。删除了union
说明符。
答案 0 :(得分:1)
条件析构函数可以通过带有模板特化的附加中间层实现。例如:
#include <type_traits>
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
class storage
{
aligned_storage_t<sizeof(T)> buf;
storage(storage&&) = delete;
public:
storage()
{
new (&buf) T{};
}
T &operator*()
{
return *static_cast<T*>(&buf);
}
void destroy()
{
(**this).~T();
}
};
template<typename T, bool destructor>
struct conditional_storage_destructor
{
storage<T> x;
};
template<typename T>
struct conditional_storage_destructor<T, true> : protected storage<T>
{
storage<T> x;
~conditional_storage_destructor()
{
x.destroy();
}
};
template<typename T>
class wrapper
{
conditional_storage_destructor<T, not is_trivially_destructible<T>::value> x;
public:
T &operator*()
{
return *(x.x);
}
};
int main()
{
static_assert(is_trivially_destructible< wrapper<int> >::value);
static_assert(not is_trivially_destructible< wrapper<vector<int>> >::value);
cout << "executed" << endl;
}