为什么 std::is_function<T> 会导致编译错误?

Why std::is_function<T> is causing compilation error?

本文关键字:编译 错误 gt function std lt 为什么 is      更新时间:2023-10-16

Hi我试图在C++11中实现一个类似C++概念的特性(C++14)。这个想法只是为std::for_each()算法编写包装函数,在这里我只检查第三个参数是否是函数。所以我写了下面的代码,但我无法按照应该的方式编译。我使用的是Ubuntu12.04和gcc4.8.1。

test_1.cpp

#include<type_traits>
#include<iostream>
#include<vector>
#include<algorithm>

void  display(int& val) {
    std::cout <<val<<std::endl;
}
template<typename T>
constexpr   bool  Is_valid_function(T& a)  {
        return std::is_function<T>::value; 
}

template<typename T>
void  check_valid_function(T& a)  {
        static_assert(Is_valid_function(a), "Not The Valid Function");
}

template <class InputIterator, class Function>
Function my_for_each(InputIterator first, InputIterator last, Function f) {
/* Concept Like Implementation to just check whether f is function or not */
            check_valid_function(f);
            return for_each(first, last, f) ;
}

void learn_static_assert_and_typetraits(void)
{
        std::vector<int> vec_x{1,2,3,4,5};
        my_for_each(vec_x.begin(), vec_x.end(), display);
    }

int main(int argc, const char* argv[]) {
        learn_static_assert_and_typetraits();
        return 0;
}

我收到以下编译错误,从中我可以看到static_assert()失败,这是不正确的,因为display是有效函数。

编译错误

test_3.cpp:在"void check_valid_function(T&)[其中T=void(*)(int&)]"的实例化中:test_3.cpp:27:26:从"Function my_for_each(InputIterator,InputIterater,Function)[其中InputIterator=__gnu_cxx::__normal_iterator>;Function=void(*)(int&)]"中需要test_3.cpp:35:50:从这里开始是必需的test_3.cpp:19:3:错误:静态断言失败:不是有效函数static_assert(Is_valid_function(a),"不是有效函数");^

然而,如果我对其他type_traits函数做同样的事情,我会得到以下错误,这是正确的,也是意料之中的。

test_2.cpp

#include<type_traits>
template<typename T>
constexpr   bool Is_floating_point(T& a) {
        return std::is_floating_point<T>::value; 
}

template<typename T>
void  f(T& a)  {
        static_assert(Is_floating_point(a), "Non-Float Type  Data");
}

void learn_static_assert_and_typetraits(void) {
        float y{10.0};
        f(y);
        int x{100};
        f(x);
}

int main(int argc, const char* argv[]) {
        learn_static_assert_and_typetraits();
        return 0;
}

编译器输出

test_2.cpp:在"void f(T&)[with T=int]"的实例化中:test_2.cpp:19:6:从这里开始需要test_2.cpp:1:3:错误:静态断言失败:非浮点类型数据static_assert(Is_floating_point(a),"非浮点类型数据");^

问题

所以,我想了解为什么我的第一个程序没有正常工作,是我的代码/理解中有错误还是其他原因。我希望以上数据足以理解我的问题。但是,如果有人想要一些额外的数据,请告诉我。

问题就在这里:

template <class InputIterator, class Function>
Function my_for_each(InputIterator first, InputIterator last, Function f)

通过:调用

my_for_each(vec_x.begin(), vec_x.end(), display);

这将(my_for_each的)Function推导为函数指针;对于

void  display(int& val)

推导出的类型为CCD_ 6。但是,类型特征std::is_function检查传递的类型是否是函数类型,而不是功能指针类型。


一种解决方案是删除指针:

template<typename T>
constexpr   bool  Is_valid_function(T& a)  {
        return std::is_function<typename std::remove_pointer<T>::type>::value; 
}

但是,正如clang++所揭示的,这仍然不够:

template<typename T>
void  check_valid_function(T& a)  {
        static_assert(Is_valid_function(a), "Not The Valid Function");
}

作为函数参数的a(即使check_valid_functionconstexpr!)是而不是编译时常量,因此它可能不会出现在常量表达式中(函数体内部)。因此,Is_valid_function(a)可能不会出现为针对static_assert的检查。可能使用类似于declval的东西,例如

static_assert(Is_valid_function( declval<T&>() ), "Not The Valid Function");

但不幸的是,declval而不是constexpr,我不知道如何编写constexpr版本。因此,您可以传递一个指针:

static_assert(Is_valid_function(static_cast<T*>(nullptr)),
              "Not a valid function");

为此,您需要重写Is_valid_function如下:

template<typename T>
constexpr   bool  Is_valid_function(T*)  {
        return std::is_function<typename std::remove_pointer<T>::type>::value; 
}

注意:这里传递的参数是指向函数的指针,但参数T*T推断为指向函数指针,与以前一样(因此签名发生了变化)。如果选择此解决方案,您可能希望在函数名称中反映这一点。


其他问题:

  1. 基于ADL的标准库算法

    return for_each(first, last, f) ;
    

    就我所见,这依赖于ADL。但是迭代器(和函数)不需要在名称空间std中(甚至对于vector::iterator等),所以您不应该依赖ADL:

    return std::for_each(first, last, f);
    
  2. 对不需要修改参数的函数使用非常量引用,例如

    constexpr   bool  Is_valid_function(T& a)
    

    如果你不需要修改一个参数,你应该通过值或常量引用来传递它,例如

    constexpr   bool  Is_valid_function(T const& a)
    
  3. "错误检查"如果此代码只是用于教育目的,则这不是问题。但是,当试图检查传递的参数是否对标准库算法有效时,检查该参数是否为函数类型是"错误检查"。您应该检查f(*first)是否形成良好。这允许函数对象,并检查参数类型是否"有效"。