为什么 std::is_function<T> 会导致编译错误?
Why std::is_function<T> is causing compilation error?
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_function
是constexpr
!)是而不是编译时常量,因此它可能不会出现在常量表达式中(函数体内部)。因此,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
推断为指向函数的指针,与以前一样(因此签名发生了变化)。如果选择此解决方案,您可能希望在函数名称中反映这一点。
其他问题:
基于ADL的标准库算法
return for_each(first, last, f) ;
就我所见,这依赖于ADL。但是迭代器(和函数)不需要在名称空间
std
中(甚至对于vector::iterator
等),所以您不应该依赖ADL:return std::for_each(first, last, f);
对不需要修改参数的函数使用非常量引用,例如
constexpr bool Is_valid_function(T& a)
如果你不需要修改一个参数,你应该通过值或常量引用来传递它,例如
constexpr bool Is_valid_function(T const& a)
"错误检查"如果此代码只是用于教育目的,则这不是问题。但是,当试图检查传递的参数是否对标准库算法有效时,检查该参数是否为函数类型是"错误检查"。您应该检查
f(*first)
是否形成良好。这允许函数对象,并检查参数类型是否"有效"。
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?