今天我遇到了这段代码(在 boost/type_index/type_index_facade.hpp中,第252-259行).
/// noexcept comparison operators for type_index_facade classes.
bool operator ==, !=, <, ... (const type_index_facade& lhs, const type_index_facade& rhs) noexcept;
/// noexcept comparison operators for type_index_facade and it's TypeInfo classes.
bool operator ==, !=, <, ... (const type_index_facade& lhs, const TypeInfo& rhs) noexcept;
/// noexcept comparison operators for type_index_facade's TypeInfo and type_index_facade classes.
bool operator ==, !=, <, ... (const TypeInfo& lhs, const type_index_facade& rhs) noexcept;
Run Code Online (Sandbox Code Playgroud)
有人能解释一下这是什么意思吗?我以前从未见过像"==,!=,<,......"之类的东西.
我们举个例子
class X
{
int value;
public:
X (int def = 0) : value (def) {}
void add (int i)
{
value += i;
}
};
Run Code Online (Sandbox Code Playgroud)
显然,该函数void X::add (int)永远不会抛出任何异常.
我的问题是,编译器是否可以分析代码并决定不生成机器代码来处理异常,即使函数没有标记为noexcept?
有人可以解释一下std :: declval是如何工作的吗?我在gcc headers/type_traits(第2248-2262行)中找到了这个实现,这是为了提高可读性而稍微清理一下:
template<typename _Tp>
struct __declval_protector
{
static const bool __stop = false;
static typename add_rvalue_reference<_Tp>::type __delegate();
};
template<typename _Tp>
typename add_rvalue_reference<_Tp>::type
declval ()
{
static_assert(__declval_protector<_Tp>::__stop, "declval() must not be used!");
return __declval_protector<_Tp>::__delegate();
}
Run Code Online (Sandbox Code Playgroud)
我不理解该部分return __declval_protector<_Tp>::__delegate(),是否为类型为T的Rvalue引用调用默认初始值设定项?另外我不明白为什么每次打电话都没有调用,因为它总是假的(它如何区分未评估的上下文和评估的上下文?).static_assertdeclval__stop
编辑: 从我现在的理解,所有这些东西相当于:
template<typenam _Tp>
struct __declval_protector
{
const bool __stop = false;
};
template<typename _Tp>
typename add_rvalue_reference<_Tp>::type
mydeclval ()
{
static_assert(__declval_protector<_Tp>::__stop, "declval() must not be used!");
}
Run Code Online (Sandbox Code Playgroud)
但当然编译器会发出我们不返回任何内容的问题.
我正在研究一个Qt项目,我决定实现一个函数,该函数接受一个指向QLabel成员函数及其参数的指针,并将其应用于某些标签.
template <class R, class ...Args>
void ClockSim::applyToLabels (R (QLabel::*f)(Args...), Args&& ...args)
{
ui->labelSingleTime->*f (std::forward<Args>(args)...);
//Repeat it for many other labels
}
Run Code Online (Sandbox Code Playgroud)
我试着用它来调用它:
applyToLabels (&QLabel::setStyleSheet, "color:red;");
Run Code Online (Sandbox Code Playgroud)
它说:
错误:没有匹配的成员函数来调用'applyToLabels'
候选模板被忽略:无法将'QLabel'与'QWidget'匹配
有解决方案吗 谢谢
我有一个QVector<float>,我需要从中获取一个指向最佳(最小)N 值的迭代器/指针数组。我该怎么做,最好使用 STL 算法?
我希望有一个列表元素,文本左侧对齐,另一个文本右侧对齐.
我试过这个:https://jsfiddle.net/dso5x75g,但它没有按照我的预期工作,因为"点"在文本上方.
我怎么能正确地做到这一点?
我的代码:
<ul>
<li>
<div style="float:left;">Pizza </div>
<div style="float:right;">€ 20,00</div>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)