斯菲奈:enable_if条件
sfinae: enable_if condition
我总是只看到enable_if与条件std::is_integral::value一起使用。
有没有办法在条件中使用另一个模板类的对象的成员函数的函数调用?我正在谈论的功能应如下所示:
bool someFunc()
{
if (QString(T::staticMetaObject.className()) == QString("Form")) {
return true;
}
return false;
}
目前,无论我尝试什么,我都无法编译它。
谢谢你帮助我。
编辑:
我的问题更清楚更多的代码和错误消息。
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <type_traits>
#include <QDebug>
template <class T>
typename std::enable_if<Form<MainWindow>::staticThis->someFunc(),bool>::type
is_smth (T* obj) { return true; }
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
form = new Form<MainWindow>;
qDebug() << is_smth(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "form.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Form<MainWindow>* form;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
表格.h
#ifndef FORM_H
#define FORM_H
#include <QObject>
class FormBase : public QObject
{
Q_OBJECT
};
template <class T>
class Form : public FormBase
{
public:
Form() {}
static Form* staticThis;
bool someFunc()
{
if (QString(T::staticMetaObject.className()) == QString(Form::staticMetaObject.className())) {
return true;
}
return false;
}
};
#endif // FORM_H
形式.cpp
#include "form.h"
#include "mainwindow.h"
Form<MainWindow>* Form::staticThis = NULL;
错误:
'Form::staticThis' 的值在常量表达式中不可用 typename std::enable_if::staticThis->someFunc(),bool>::type ^
我不想在enable_if std 或其他函数的情况下使用。我想使用自己的函数。而且我不知道如何让它工作。也许这就是为什么你会认为代码有点忙。但我认为它现在应该显示我正在努力实现的目标。
再次感谢
不确定您要查找的内容,但以下是使用成员函数的 sfinae 示例:
#include<type_traits>
template<typename T>
std::enable_if_t<std::is_member_function_pointer<T>::value>
foo() { }
struct S {
void bar() { }
int i;
};
int main() {
// compiles, for it is a member function
foo<decltype(&S::bar)>();
// does not compile, for it is not a member function
//foo<decltype(&S::i)>();
}
编辑
编辑 OP 后,我用一个最小的工作示例更新答案,该示例展示了如何以他想要的方式使用 sfinae。
为此,至少出于显而易见的原因,必须constexpr
成员函数。
#include<type_traits>
struct S {
static constexpr bool bar(bool b) { return b; }
};
template<bool b>
std::enable_if_t<S::bar(b)>
foo() { }
int main() {
foo<true>();
// fails to compile because of sfinae
// foo<false>();
}
OP 的示例远非最小示例,我想不值得修复它。
相反,上面的一个足以解释解决方案背后的想法。
编辑
如评论中所述,C++11对constexpr
功能有更严格的限制。
相反,自 C++14 以来,您可以在 constexpr
函数中使用更复杂的语句。
例如,下面的代码使用 -std=c++14
编译,而不使用 -std=c++11
编译:
#include<type_traits>
struct T {
constexpr T(char c): ch{c} { }
char ch;
};
struct S {
static constexpr bool bar(T&& t) {
if(t.ch == 'y') {
return static_cast<unsigned int>(t.ch) % 2 == 0;
} else {
return true;
}
}
};
template<char c>
std::enable_if_t<S::bar(T{c})>
foo() { }
int main() {
foo<'x'>();
// fails to compile because of sfinae
// foo<'y'>();
}
- 将按位if条件转换为普通if条件
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么这个if(s[j]==s[i] && i>0)条件不适用于这个[{()}]
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- 当我的 if 条件计算结果为 false 时,我的 else 块将不会执行
- 使用枚举作为条件,if 条件将返回什么,真或假?
- 为什么循环没有中断,并且 if 条件没有按预期工作?
- 使用 if 条件对数字进行分组
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- 调用一个小函数两次(例如在if条件和主体中)比将结果存储在局部变量中更可取
- 在if条件下,右或左改变值的相等性检查是否有任何区别
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- 将子字符串与字符串数组进行比较的 IF 条件会在C++中产生错误
- 在 constexpr-if 条件下比较 constexpr 函数参数会导致错误
- 带有逻辑 OR 运算符的 if 条件的缩写?
- 为什么我可以将 ifstream 文件置于 if 条件?
- 如何将特征张量的值作为'if'条件表达式
- 在 c++ 中检查 if 条件中的向量位置范围
- 如何在Char数组中搜索Char在IF条件语句中
- 未正确评估'If'条件