C++ c+的正确返回类型+;功能?

C++ c+的正确返回类型+;功能?,c++,C++,此函数应具有什么类型标识符?auto和“long long”(教师建议)给出编译器错误。我还尝试了chrono::high_resolution_clock::time_point,它也给出了一个错误 auto ComputeDuration(chrono::high_resolution_clock::time_point start_time, chrono::high_resolution_clock::time_point end_time) { return end_time

此函数应具有什么类型标识符?auto和“long long”(教师建议)给出编译器错误。我还尝试了chrono::high_resolution_clock::time_point,它也给出了一个错误

auto ComputeDuration(chrono::high_resolution_clock::time_point start_time, chrono::high_resolution_clock::time_point end_time) {
     return end_time - start_time;
}

auto
只是“编译器请为我推断出这种类型”的语法糖。它不会改变实际的类型


无论您是写出函数返回的实际类型,还是使用
auto
(如果可以推断)都不会改变任何内容。函数仍然返回相同的类型。

auto
只是“编译器请为我推断此类型”的语法糖。它不会改变实际的类型


无论您是写出函数返回的实际类型,还是使用
auto
(如果可以推断)都不会改变任何内容。该函数仍然返回相同的类型。

将使用以下运算符从另一个
高分辨率时钟::时间点中减去一个

template<class Clock, class Duration1, class Duration2>
constexpr std::common_type_t<Duration1, Duration2>
    operator-( const time_point<Clock, Duration1>& end_time,
               const time_point<Clock, Duration2>& start_time);
由于您的函数采用相同
时间点类型的两个参数,因此返回的类型为:

std::chrono::high_resolution_clock::duration

如果要在C++11模式下使用
auto
,则需要提供一个尾随返回类型:

using namespace std::chrono;

// I suggest taking the arguments by const& instead of by value
auto ComputeDuration(
    const high_resolution_clock::time_point& start_time,
    const high_resolution_clock::time_point& end_time) -> decltype(end_time-start_time)
{
     return end_time - start_time;
}

返回值的类型将是相同的:
high\u resolution\u clock::duration

将使用以下运算符从另一个
high\u resolution\u clock::time\u point
中减去一个

template<class Clock, class Duration1, class Duration2>
constexpr std::common_type_t<Duration1, Duration2>
    operator-( const time_point<Clock, Duration1>& end_time,
               const time_point<Clock, Duration2>& start_time);
由于您的函数采用相同
时间点类型的两个参数,因此返回的类型为:

std::chrono::high_resolution_clock::duration

如果要在C++11模式下使用
auto
,则需要提供一个尾随返回类型:

using namespace std::chrono;

// I suggest taking the arguments by const& instead of by value
auto ComputeDuration(
    const high_resolution_clock::time_point& start_time,
    const high_resolution_clock::time_point& end_time) -> decltype(end_time-start_time)
{
     return end_time - start_time;
}

<返回值的类型将是相同的:<代码> Hyl RealPrimeSythCalp::持续时间

这就是我所想的但TooRo::HyjyRealPosithOnCalth::TimeSoin在长Load的位置也给出编译器错误。你正在编译哪个C++标准?我明白了。我用的是c++11。“自动”要求c++14或更高版本。如果出现错误,应将其包括在问题中。是否仍使用c++11?您是否尝试在返回类型中更改“TimeApple Posits”到“Survivin”?这就是我所想的但TeaO::HythOnRealPosithOnCalth::TimeSyPoin在长Load的位置也给出了编译器错误。您正在编译哪个C++标准?我明白了。我用的是c++11。“自动”要求c++14或更高版本。如果出现错误,应将其包括在问题中。是否仍使用c++11?你试过在返回类型中将“时间点”改为“持续时间”吗?@MooingDuck我试着让它更清楚。我希望它没有产生相反的效果。这更具描述性。好job@MooingDuck非常感谢!(也感谢雷米的帮助:-)@MooingDuck我试着说得更清楚些。我希望它没有产生相反的效果。这更具描述性。好job@MooingDuck非常感谢!(也感谢雷米的帮助:-)