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++模板元编程。我知道stackoverflow提供了这个特定问题的解决方案,我感兴趣的是理解为什么这个解决方案不起作用。代码如下: template < std::size_t... Ns , typename... Ts > auto tail_impl( std::index_sequence<Ns...> , std::tuple<Ts...> t ) { return std::make_tuple( std::get<Ns+1u>(t)... ); } template <class F, class... R > tuple<R...> tail( std::tuple<F,R...> t ) { return tail_impl( std::make_index_sequence<sizeof...(R)>() , t ); } template<class X, class F, class... R> constexpr bool check_for_type(tuple<F,R...> t) { if constexpr(is_same<F,X>::value) { return true; } return check_for_type<X>(tail(t)); } template<class X> constexpr bool check_for_type(tuple<> t) { return false; } int main( int argc, const char *argv) { auto t2 = make_tuple(4,"qw", 6.5); double f = check_for_type<double>(t2); return 0; } 模板 自动尾部执行(std::index\u序列,std::tuple t) { 返回std::make_tuple(std::get(t)…); } 模板 元组尾(std::tuple t) { 返回tail_impl(std::make_index_sequence(),t); } 模板 constexpr bool检查类型(元组t){ 如果constexpr(相同::值){ 返回true; } 返回检查_类型(tail(t)); } 模板 constexpr bool检查类型(元组t){ 返回false; } int main(int argc,const char*argv){ 自动t2=生成元组(4,“qw”,6.5); 双f=检查_类型(t2); 返回0; }_C++_Templates_Metaprogramming_Template Meta Programming - Fatal编程技术网

检查元组是否包含特定类型的元素 我试图通过实现一些函数来学习C++模板元编程。我知道stackoverflow提供了这个特定问题的解决方案,我感兴趣的是理解为什么这个解决方案不起作用。代码如下: template < std::size_t... Ns , typename... Ts > auto tail_impl( std::index_sequence<Ns...> , std::tuple<Ts...> t ) { return std::make_tuple( std::get<Ns+1u>(t)... ); } template <class F, class... R > tuple<R...> tail( std::tuple<F,R...> t ) { return tail_impl( std::make_index_sequence<sizeof...(R)>() , t ); } template<class X, class F, class... R> constexpr bool check_for_type(tuple<F,R...> t) { if constexpr(is_same<F,X>::value) { return true; } return check_for_type<X>(tail(t)); } template<class X> constexpr bool check_for_type(tuple<> t) { return false; } int main( int argc, const char *argv) { auto t2 = make_tuple(4,"qw", 6.5); double f = check_for_type<double>(t2); return 0; } 模板 自动尾部执行(std::index\u序列,std::tuple t) { 返回std::make_tuple(std::get(t)…); } 模板 元组尾(std::tuple t) { 返回tail_impl(std::make_index_sequence(),t); } 模板 constexpr bool检查类型(元组t){ 如果constexpr(相同::值){ 返回true; } 返回检查_类型(tail(t)); } 模板 constexpr bool检查类型(元组t){ 返回false; } int main(int argc,const char*argv){ 自动t2=生成元组(4,“qw”,6.5); 双f=检查_类型(t2); 返回0; }

检查元组是否包含特定类型的元素 我试图通过实现一些函数来学习C++模板元编程。我知道stackoverflow提供了这个特定问题的解决方案,我感兴趣的是理解为什么这个解决方案不起作用。代码如下: template < std::size_t... Ns , typename... Ts > auto tail_impl( std::index_sequence<Ns...> , std::tuple<Ts...> t ) { return std::make_tuple( std::get<Ns+1u>(t)... ); } template <class F, class... R > tuple<R...> tail( std::tuple<F,R...> t ) { return tail_impl( std::make_index_sequence<sizeof...(R)>() , t ); } template<class X, class F, class... R> constexpr bool check_for_type(tuple<F,R...> t) { if constexpr(is_same<F,X>::value) { return true; } return check_for_type<X>(tail(t)); } template<class X> constexpr bool check_for_type(tuple<> t) { return false; } int main( int argc, const char *argv) { auto t2 = make_tuple(4,"qw", 6.5); double f = check_for_type<double>(t2); return 0; } 模板 自动尾部执行(std::index\u序列,std::tuple t) { 返回std::make_tuple(std::get(t)…); } 模板 元组尾(std::tuple t) { 返回tail_impl(std::make_index_sequence(),t); } 模板 constexpr bool检查类型(元组t){ 如果constexpr(相同::值){ 返回true; } 返回检查_类型(tail(t)); } 模板 constexpr bool检查类型(元组t){ 返回false; } int main(int argc,const char*argv){ 自动t2=生成元组(4,“qw”,6.5); 双f=检查_类型(t2); 返回0; },c++,templates,metaprogramming,template-meta-programming,C++,Templates,Metaprogramming,Template Meta Programming,此模板应检查元组是否包含特定类型的元素,但编译它时会出现以下错误: > clang++ t.cpp -std=c++17 t.cpp:45:12: error: call to function 'check_for_type' that is neither visible in the template definition nor found by argument-dependent lookup return check_for_type<X>(t

此模板应检查元组是否包含特定类型的元素,但编译它时会出现以下错误:

> clang++ t.cpp -std=c++17
t.cpp:45:12: error: call to function 'check_for_type' that is neither visible in the
      template definition nor found by argument-dependent lookup
    return check_for_type<X>(tail(t));
           ^
t.cpp:45:12: note: in instantiation of function template specialization
      'check_for_type<double, double>' requested here
t.cpp:45:12: note: in instantiation of function template specialization
      'check_for_type<double, const char *, double>' requested here
t.cpp:66:16: note: in instantiation of function template specialization
      'check_for_type<double, int, const char *, double>' requested here
    double f = check_for_type<double>(t2);
               ^
t.cpp:58:16: note: 'check_for_type' should be declared prior to the call site
constexpr bool check_for_type(tuple<> t) {
               ^
1 error generated.
>clang++t.cpp-std=c++17
t、 cpp:45:12:错误:调用在
模板定义未通过参数相关查找找到
返回检查_类型(tail(t));
^
t、 cpp:45:12:注意:在函数模板专门化的实例化中
此处请求的“检查\u类型”
t、 cpp:45:12:注意:在函数模板专门化的实例化中
此处请求的“检查\u类型”
t、 cpp:66:16:注意:在函数模板专门化的实例化中
此处请求的“检查\u类型”
双f=检查_类型(t2);
^
t、 cpp:58:16:注意:“检查类型”应在呼叫站点之前声明
constexpr bool检查类型(元组t){
^
生成1个错误。
这段代码怎么了

模板
constexpr bool检查类型(元组t){
如果constexpr(相同::值){
返回true;
} 
返回检查_类型(tail(t));
}
在声明类型(…)之前,您正在调用
检查类型。您的错误消息指出:

t.cpp:58:16:注意:应在调用站点之前声明“检查类型”
constexpr bool检查类型(元组t){
一旦您这样做,代码:

//将此函数放在第一位
模板
constexpr bool检查类型(元组t){
返回false;
}
模板
constexpr bool检查类型(元组t){
如果constexpr(相同::值){
返回true;
}
//在这里,编译器查找'check_for_type',但没有找到
//只需一个显式类型参数即可调用的重载。
返回检查_类型(tail(t));
}
模板
constexpr bool检查类型(元组t){
如果constexpr(相同::值){
返回true;
} 
返回检查_类型(tail(t));
}
在声明类型(…)
之前,您正在调用
检查类型。您的错误消息指出:

t.cpp:58:16:注意:应在调用站点之前声明“检查类型”
constexpr bool检查类型(元组t){
一旦您这样做,代码:

//将此函数放在第一位
模板
constexpr bool检查类型(元组t){
返回false;
}
模板
constexpr bool检查类型(元组t){
如果constexpr(相同::值){
返回true;
}
//在这里,编译器查找'check_for_type',但没有找到
//只需一个显式类型参数即可调用的重载。
返回检查_类型(tail(t));
}

由于您在代码中使用的是c++17,我认为有必要指出,有很多新工具可以避免生成此类递归模板

你可以把整个事情浓缩成这样:

#include <iostream>
#include <type_traits>
#include <tuple>

template <typename T1, typename... T2>
constexpr bool check_for_type(std::tuple<T2...>) {
    return std::disjunction_v<std::is_same<T1, T2>...>;
}

int main() {
    std::tuple<int, char, bool> tup;
    std::cout << check_for_type<char>(tup) << '\n';
    std::cout << check_for_type<float>(tup) << std::endl;
    return 0;
}
#包括
#包括
#包括
模板
constexpr bool检查类型(std::tuple){
返回std::析取v;
}
int main(){
std::tuple-tup;

std::cout因为您在代码中使用的是c++17,所以我认为有必要指出,有很多新工具可以避免生成此类递归模板

你可以把整个事情浓缩成这样:

#include <iostream>
#include <type_traits>
#include <tuple>

template <typename T1, typename... T2>
constexpr bool check_for_type(std::tuple<T2...>) {
    return std::disjunction_v<std::is_same<T1, T2>...>;
}

int main() {
    std::tuple<int, char, bool> tup;
    std::cout << check_for_type<char>(tup) << '\n';
    std::cout << check_for_type<float>(tup) << std::endl;
    return 0;
}
#包括
#包括
#包括
模板
constexpr bool检查类型(std::tuple){
返回std::析取v;
}
int main(){
std::tuple-tup;

std::cout@Justin
tail
获取一个元组并返回另一个不包含第一个元素的输入元组。我将编辑代码以包含
tail
定义。@Justin
tail
获取一个元组并返回另一个不包含第一个元素的输入元组。我将编辑代码以包含
tail
定义。这是answers(以最好的方式IMHO)回答另一个问题:但它不属于这里,因为OP特别问他们的代码为什么不工作。这回答了(以最好的方式IMHO)另一个问题:但它不属于这里,因为OP特别问他们的代码为什么不工作。
// Put this function first
template<class X>
constexpr bool check_for_type(tuple<> t) {
    return false;
}

template<class X, class F, class... R>
constexpr bool check_for_type(tuple<F,R...> t) {
    if constexpr(is_same<F,X>::value) {
        return true;
    }
    // Right here, the compiler looks up `check_for_type` and doesn't find
    // an overload that can be called with just one explicit type parameter.
    return check_for_type<X>(tail(t));
}
#include <iostream>
#include <type_traits>
#include <tuple>

template <typename T1, typename... T2>
constexpr bool check_for_type(std::tuple<T2...>) {
    return std::disjunction_v<std::is_same<T1, T2>...>;
}

int main() {
    std::tuple<int, char, bool> tup;
    std::cout << check_for_type<char>(tup) << '\n';
    std::cout << check_for_type<float>(tup) << std::endl;
    return 0;
}