至于第二个问题,就这样吧:
template<typename ...Args>
void foo () {
fun({Args::value...});
}
该机制非常直观:您创建一个包含扩展Args::value
模式的初始化器列表,从而(在您的情况下)解析为{ A::value, B::value, C::value, D::value }
.
这是一个完整的程序:
#include <string>
#include <iostream>
void fun (const std::initializer_list<std::string>& strings) {
for(auto s : strings)
{
std::cout << s << " ";
}
}
template<typename ...Args>
void foo () {
fun({Args::value...});
}
struct A { static std::string value; };
struct B { static std::string value; };
struct C { static std::string value; };
struct D { static std::string value; };
std::string A::value = "Hello";
std::string B::value = "World";
std::string C::value = "of";
std::string D::value = "Variadic Templates";
int main()
{
foo<A, B, C, D>(); // where A, B, C, D are classes
}
这是一个活生生的例子。
至于静态断言,您可以编写一个类型特征来确定某个类型是否具有成员变量value
:
template<typename T, typename V = bool>
struct has_value : std::false_type { };
template<typename T>
struct has_value<T,
typename std::enable_if<
!std::is_same<decltype(std::declval<T>().value), void>::value,
bool
>::type
> : std::true_type
{
typedef decltype(std::declval<T>().value) type;
};
然后,您可以这样使用它:
template<typename T>
struct check_has_value
{
static_assert(has_value<T>::value, "!");
};
template<typename ...Args>
void foo () {
auto l = { (check_has_value<Args>(), 0)... };
fun({Args::value...});
}
这是一个成功检查的实时示例(所有类都有一个value
数据成员)。这是一个不成功检查的实时示例D
(调用类的数据成员values
)