我有一段简单的代码,用来包装struct timespec
并添加了它的最小值和最大值的静态成员。
#include <sys/stat.h>
#include <limits>
struct Timespec : public timespec {
Timespec() :timespec() {};
Timespec(decltype(tv_sec) s,
decltype(tv_nsec) ns
) {
tv_sec = s;
tv_nsec = ns;
}
static const Timespec max;
static const Timespec min;
};
const Timespec Timespec::min = Timespec(0,0);
const Timespec Timespec::max = Timespec(
std::numeric_limits<decltype((timespec().tv_sec))>::max(),
std::numeric_limits<decltype((timespec().tv_nsec))>::max()
);
代码可以成功编译,但是如果我把末尾两行的decltype((timespec()/*...*/
替换为decltype((Timespec()/*...*/
,就会出现以下错误:
$ make timespec.o
g++ -std=c++0x -c -o timespec.o timespec.cc
In file included from timespec.cc:2:0:
/usr/include/c++/4.8/limits: In instantiation of ‘static constexpr _Tp std::numeric_limits<_Tp>::max() [with _Tp = long int&]’:
timespec.cc:18:55: required from here
/usr/include/c++/4.8/limits:313:48: error: value-initialization of reference type ‘long int&’
max() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
^
/usr/include/c++/4.8/limits:313:51: error: body of constexpr function ‘static constexpr _Tp std::numeric_limits<_Tp>::max() [with _Tp = long int&]’ not a return-statement
max() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
^
它应该这么做吗?
std::remove_reference_t
。 - Kerrek SB