请考虑以下示例:
#include <sstream>
template <typename T>
inline std::string to_string(T const & op) {
std::ostringstream result;
result << op;
return result.str();
}
Run Code Online (Sandbox Code Playgroud)
如果我要返回结果,而不是result.str()它将自动成为右值.不是结果中包含的字符串(我假设).我的期望是它被复制并且副本作为右值返回.
所以我的问题是,合法的是:
return std::move(result.str());
Run Code Online (Sandbox Code Playgroud)
我认为它是,期望流留下一个有效的空字符串.但我不确定实际做到这一点.
我有一个表示运行时上下文并构建树的类,树根保存在unique_ptr.在构建树时,我想要提取树.这是它的外观(不可运行,这不是一个调试问题):
class Context {
private:
std::unique_ptr<Node> root{new Node{}};
public:
// imagine a constructor, attributes and methods to build a tree
std::unique_ptr<Node> extractTree() {
return std::move(this->root);
}
};
Run Code Online (Sandbox Code Playgroud)
所以我曾经std::move()从Context实例中提取根节点.
然而,有使用的替代方案,std::move()例如:
std::unique_ptr<Node> extractTree() {
// This seems less intuitive to me
return std::unique_ptr<Node>{this->root.release()};
}
Run Code Online (Sandbox Code Playgroud)
是std::move()最好的选择吗?
我在样式中有一个模板函数:
template <int Exponent> DERIVED_TYPE pow(TYPE const x);
Run Code Online (Sandbox Code Playgroud)
此函数在模板结构中作为友元函数内联定义:
template <ARGUMENTS>
struct unit {
typedef unit<ARGUMENTS> type;
....
template <int Exponent>
friend constexpr unit_pow_t<type, Exponent> pow(type const x) { ... }
};
Run Code Online (Sandbox Code Playgroud)
这是因为将具有单位的值与功率一起取决于该值必须改变单位.
当我尝试使用它省略Exponent时,我可以看到编译器考虑匹配的候选者:
src/model/Tool.cpp:113:3: error: no matching function for call to 'pow'
pow(1._m);
^~~
src/model/../units/Units.hpp:2266:46: note: candidate template ignored: couldn't infer template argument 'Exponent'
friend constexpr unit_pow_t<type, Exponent> pow(type const x) {
^
/usr/include/math.h:255:8: note: candidate function not viable: requires 2 arguments, but 1 was provided
double pow(double, double); …Run Code Online (Sandbox Code Playgroud) 通常在定义中保留未命名的未使用的函数参数.
例如:
inline std::string operator "" _s(char const * const op, size_t const) {
return {op};
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,doxygen 1.8.12似乎错误(最后一部分)参数名称的类型:
warning: The following parameters of … are not documented:
parameter 'const'
Run Code Online (Sandbox Code Playgroud)
有没有办法摆脱这个警告而不WARN_IF_UNDOCUMENTED关闭和没有包装参数\cond,\endcond?
我有一个简单的递归模板,它实现了欧几里得算法(的优化版本)。Doxygen 对此抱怨道:
/usr/home/kamikaze/stark/Yggdrasil/src/units/Units.hpp:335: warning: Detected potential recursive class relation between class units::euclid and base class units::euclid< Rhs, Lhs%Rhs >!
/usr/home/kamikaze/stark/Yggdrasil/src/units/Units.hpp:335: warning: Detected potential recursive class relation between class units::euclid and base class euclid< Rhs, Lhs%Rhs >!
/usr/home/kamikaze/stark/Yggdrasil/src/units/Units.hpp:335: warning: Detected potential recursive class relation between class units::euclid and base class units::euclid< Rhs, Lhs%Rhs >!
/usr/home/kamikaze/stark/Yggdrasil/src/units/Units.hpp:335: warning: Detected potential recursive class relation between class units::euclid and base class euclid< Rhs, Lhs%Rhs >!
Run Code Online (Sandbox Code Playgroud)
我很惊讶为什么这是一个投诉/警告。我认为递归类型是常见且合法的。它也是众多递归模板之一,但也是 doxygen 唯一抱怨的一个。令我惊讶的是,我只发现 doxygen 错误检测递归的类似问题。
如果您有兴趣,这里是代码:
/**
* Implements Euclid's …Run Code Online (Sandbox Code Playgroud) 我有一个容器,并希望依赖于使用我的库的人来确保函数可用于基础value_type(即将到来的示例中的pow()).我希望编译器根据其签名在具有相同名称的成员函数内使用该函数.
我试图创建一个最小的例子:
#include <iostream>
#include <cmath>
using std::pow;
template <typename T>
struct container {
T value;
container<T> pow(T const exp) const {
return {pow(this->value, exp)};
}
};
int main() {
container<double> value{81.};
std::cout << value.value << "^0.25 = " << value.pow(.25).value << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所述容器<>提供POW()方法,是应该依靠POW()是可从在全局命名空间的基础类型.
这应该有助于使用自定义数字类型.即图书馆用户应该能够定义他们自己的类型,类似于数字,并为其类型提供pow()函数,使其容器<>兼容.
问题,clang和gcc都没有从全局命名空间中获取函数:
c++ -std=c++11 pow.cpp -o pow
pow.cpp:11:28: error: too many arguments to function call, expected single argument 'exp', …Run Code Online (Sandbox Code Playgroud) 可重复的例子:
#include <type_traits>
static_assert(std::is_constructible_v<int[2], int, int>, "fails against my expectations");
Run Code Online (Sandbox Code Playgroud)
我用clang 5和gcc 7测试了这个.