Tôi muốn các tham số mẫu variadic phải là duy nhất. Tôi biết khi nào đa thừa kế, thừa kế lớp giống hệt nhau không được phép.kiểm tra các tham số mẫu variadic cho tính duy nhất
struct A{};
struct B: A, A{}; // error
Sử dụng quy tắc này, tôi đã tạo một mã nhỏ.
#include <type_traits>
template< class T> struct id{};
template< class ...T> struct base_all : id<T> ... {};
template< class ... T>
struct is_unique
{
template< class ... U>
static constexpr bool test(base_all<U...> *) noexcept { return true; }
template< class ... U>
static constexpr bool test(...) noexcept { return false;}
static constexpr bool value = test<T...>(0);
};
int main()
{
constexpr bool b = is_unique<int, float, double>::value; // false -- Why?
constexpr bool c = is_unique< int, char, int>::value; // false
static_assert(b == true && c == false , "!");// failed.
}
Nhưng chương trình của tôi không hoạt động như tôi mong đợi. Chuyện gì vậy?
// UPDATE: // Cảm ơn, tôi sửa chữa lỗi của tôi: //
// #include <type_traits>
// #include <cstddef>
//
// template< class ... U> struct pack{};
//
// template< class T> struct id{};
// template< class T> struct base_all;
// template< class ... T> struct base_all< pack<T...> > : id<T> ... {};
//
//
//
// template< class ... T>
// struct is_unique
// {
// template< class P, std::size_t = sizeof(base_all<P>) >
// struct check;
//
// template< class ...U>
// static constexpr bool test(check< pack<U...> > *) noexcept { return true;}
//
// template< class ... U>
// static constexpr bool test(...)noexcept { return false;}
//
// static constexpr bool value = test<T...>(0);
// };
//
// int main()
// {
// constexpr bool b = is_unique<int, float, double>::value; // true
// constexpr bool c = is_unique< int, char, int>::value; // false
//
// static_assert(b == true && c == false , "!");// success.
// }
//
Q: ai đó có thể giải thích, lý do tại sao nó thất bại?
CẬP NHẬT 2: Bản cập nhật trước của tôi là bất hợp pháp :)). Hình thức pháp lý, nhưng nó đã biên soạn thời gian O (N).
#include <cstddef>
#include <iostream>
#include <type_traits>
namespace mpl
{
template< class T > using invoke = typename T :: type ;
template< class C, class I, class E > using if_t = invoke< std::conditional< C{}, I, E> >;
template< class T > struct id{};
struct empty{};
template< class A, class B > struct base : A, B {};
template< class B , class ... > struct is_unique_impl;
template< class B > struct is_unique_impl<B>: std::true_type{};
template< class B, class T, class ... U>
struct is_unique_impl<B, T, U...> : if_t< std::is_base_of< id<T>, B>, std::false_type, is_unique_impl< base<B,id<T>>, U...> >{};
template< class ...T >struct is_unique : is_unique_impl< empty, T ... > {};
} // mpl
int main()
{
constexpr bool b = mpl::is_unique<int, float, double>::value;
constexpr bool c = mpl::is_unique< int, char, int > :: value;
static_assert(b == true , "!");
static_assert(c == false, "!");
return 0;
}
+1 cho ý tưởng – Asaf