我有一個類可以接收相同類型的可變參數模板。每種類型有一個嵌套的值,它應該是唯一的:靜態斷言可變參數模板的嵌套值是唯一的
template <SomeEnum _se, int _val>
struct Pack
{
enum {val_se = _se};
enum {val = _val};
};
int main()
{
TypeMe<Pack<A_SE, 1>, Pack<B_SE, 2>, Pack<C_SE, 3>> tm_abc; // OK
TypeMe<Pack<A_SE, 1>, Pack<B_SE, 2>, Pack<A_SE, 3>> tm_aba; // Should fail (first and last Pack are templated with A_SE)
(void)tm_abc;
(void)tm_aba;
return (0);
}
整個測試代碼:
#include <cstdio>
template <typename ... ArgPacks>
class TypeMe
{
public:
TypeMe();
private:
template <typename ... APs>
void cycleAPs();
template <typename AP>
void cycleAP();
};
template <typename ... ArgPacks>
TypeMe<ArgPacks...>::TypeMe()
{
// Maybe the static assertion should go here
cycleAPs<ArgPacks...>();
}
template <typename ... ArgPacks>
template <typename ... APs>
void TypeMe<ArgPacks...>::cycleAPs()
{
int _[] = {0, (cycleAP<APs>(), 0)...};
(void)_;
return ;
}
template <typename ... ArgPacks>
template <typename AP>
void TypeMe<ArgPacks...>::cycleAP()
{
printf("SomeEnum = %d, Val = %d\n", static_cast<int>(AP::val_se), AP::val);
return ;
}
enum SomeEnum
{
A_SE,
B_SE,
C_SE,
MAX
};
template <SomeEnum _se, int _val>
struct Pack
{
enum {val_se = _se};
enum {val = _val};
};
int main()
{
TypeMe<Pack<A_SE, 1>, Pack<B_SE, 2>, Pack<C_SE, 3>> tm_abc; // OK
TypeMe<Pack<A_SE, 1>, Pack<B_SE, 2>, Pack<A_SE, 3>> tm_aba; // Should fail (first and last Pack are templated with A_SE)
(void)tm_abc;
(void)tm_aba;
return (0);
}
有沒有一種方法,在C++ 0x中,檢查在編譯時,每Pack :: val_se是不同的?或與C + + 11?
感謝您閱讀
編輯:
相同的代碼,但@MadScientist解答,非常感謝
#include <cstdio>
template <typename ...Ts>
struct are_mutually_different;
template <typename T>
struct are_mutually_different<T>
{
static const bool value = true;
};
template <typename T1, typename T2, typename ...Ts>
struct are_mutually_different<T1, T2, Ts...>
{
static const bool value = (T1::val_se != T2::val_se) &&
are_mutually_different<T1, Ts...>::value &&
are_mutually_different<T2, Ts...>::value;
};
template <typename ... ArgPacks>
class TypeMe
{
public:
TypeMe();
private:
template <typename ... APs>
void cycleAPs();
template <typename AP>
void cycleAP();
};
template <typename ... ArgPacks>
TypeMe<ArgPacks...>::TypeMe()
{
static_assert(are_mutually_different<ArgPacks...>::value, "!"); // <3
// Maybe the static assertion should go here
cycleAPs<ArgPacks...>();
}
template <typename ... ArgPacks>
template <typename ... APs>
void TypeMe<ArgPacks...>::cycleAPs()
{
int _[] = {0, (cycleAP<APs>(), 0)...};
(void)_;
return ;
}
template <typename ... ArgPacks>
template <typename AP>
void TypeMe<ArgPacks...>::cycleAP()
{
printf("SomeEnum = %d, Val = %d\n", static_cast<int>(AP::val_se), AP::val);
return ;
}
enum SomeEnum
{
A_SE,
B_SE,
C_SE,
MAX
};
template <SomeEnum _se, int _val>
struct Pack
{
enum {val_se = _se};
enum {val = _val};
};
int main()
{
TypeMe<Pack<A_SE, 1>, Pack<B_SE, 2>, Pack<C_SE, 3>> tm_abc; // OK
// TypeMe<Pack<A_SE, 1>, Pack<B_SE, 2>, Pack<A_SE, 3>> tm_aba; // Should fail (first and last Pack are templated with A_SE)
(void)tm_abc;
// (void)tm_aba;
return (0);
}
「的C++ 0x」是不是真的* *的事情,這只是對C++ 11在使用名稱。 –
是的我不確定這個:/但我的目標平臺之一需要用-std = C++編譯0x –
這是一個不完整的C++ 11支持的指標,所以如果你去C++ 11路由。例如,流的移動語義可能不起作用。 –