Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何访问参数包中的第一个参数?_C++_Templates_Variadic Templates - Fatal编程技术网

C++ 如何访问参数包中的第一个参数?

C++ 如何访问参数包中的第一个参数?,c++,templates,variadic-templates,C++,Templates,Variadic Templates,是否可以在编译时静态地“展开”参数列表,在每个“展开”步骤中使用一个参数?我认为可变模板是与部分模板专门化相结合的方式,但我无法运行此示例: #include <iostream> char static const text1[] = "Foo"; char static const text2[] = "FooBar"; template <char const * TEXT, unsigned int N, char const *... REST, unsigned

是否可以在编译时静态地“展开”参数列表,在每个“展开”步骤中使用一个参数?我认为可变模板是与部分模板专门化相结合的方式,但我无法运行此示例:

#include <iostream>

char static const text1[] = "Foo";
char static const text2[] = "FooBar";

template <char const * TEXT, unsigned int N, char const *... REST, unsigned int... Ns>
void doStuff() {
    std :: cout << TEXT << "-" << N << std :: endl;
    doStuff<REST..., Ns...>();
} 

template <char const * TEXT, unsigned int N>
void doStuff() {
    std :: cout << TEXT << std :: endl;
} 

void doStuff() {}

int main() {
    doStuff<text1,3,text2,5>();
    return 0;
}

在C++17中,您可以执行以下操作

template <char const * TEXT, unsigned int N>
void doStuff() {
    std::cout << TEXT << "-" << N << std::endl;
} 

template <auto v1, auto v2, auto ... values>
void doStuff()
{
    std :: cout << v1 << "-" << v2 << std :: endl;
    doStuff<values...>();
}
模板
void doStuff(){

std::cout您可以使用它们作为参数来解决此问题:

#include <iostream>
#include<type_traits>

char static const text1[] = "Foo";
char static const text2[] = "FooBar";

constexpr void doStuff() {}

template <typename T, typename U, typename... O>
constexpr
std::enable_if_t<
    std::is_same<T, const char *>::value
    and std::is_same<U, int>::value
> doStuff(T str, U num, O... o) {
    std :: cout << str << "-" << num << std :: endl;
    doStuff(o...);
}

int main() {
    doStuff(text1,3,text2,5);
    return 0;
}
#包括
#包括
char static const text1[]=“Foo”;
char static const text2[]=“FooBar”;
constexpr void doStuff(){}
模板
常量表达式
std::如果启用,则启用<
std::值是否相同
和std::值是否相同
>多斯塔夫(T街、U街、O街、O街){

std::您能否将
const char*
N
打包成一个类型,以拥有此打包类型的序列?是的,我可以,但是我想向函数传递一些额外的参数,例如
模板void doStuff(char test){}
嗯,+1对于
自动
,我喜欢C++17。谢谢你的提示。但是,现在你可以一步直接解压列表。有没有办法为每个函数提供额外的参数,也许可以递归地执行?类似于
模板void doStuff(char test){}
template<const char* S, int N>
struct pairValue {
     static constexpr const char* s = S;
     static constexpr int n = N;
};

template <typename ... Ts>
void doStuff()
{
    const int dummy[] = {0, ((std::cout << Ts::s << "-" << Ts::n << std::endl), 0)...};
    static_cast<void>(dummy); // Avoid warning for unused variable.
}
 doStuff<pairValue<text1, 3>, pairValue<text2, 5>>();
#include <iostream>
#include<type_traits>

char static const text1[] = "Foo";
char static const text2[] = "FooBar";

constexpr void doStuff() {}

template <typename T, typename U, typename... O>
constexpr
std::enable_if_t<
    std::is_same<T, const char *>::value
    and std::is_same<U, int>::value
> doStuff(T str, U num, O... o) {
    std :: cout << str << "-" << num << std :: endl;
    doStuff(o...);
}

int main() {
    doStuff(text1,3,text2,5);
    return 0;
}