C ++ decltype(auto)的const引用返回类型

时间:2018-04-27 22:01:35

标签: c++ c++11 c++17

这是我的测试程序:

#include <iostream>
#include <typeinfo>
using namespace std;

template <typename T>
decltype(auto) foo(T& arg)
{
    return arg;
}

int main()
{
    const int x = 10;
    int & y = const_cast<int&>(foo(x));
    y = 20;
    cout << "x: " << &x << " " << x << endl;
    cout << "y: " << &y << " " << y << endl;
}

输出:

g++ -std=c++17 main.cpp  && ./a.out
x: 0x7ffee31f4a6c 10
y: 0x7ffee31f4a6c 20

为什么x的值foo没有更改,即使const int&已将const_cast更改为int& x

因为从输出中看起来y string delimiter = ","; string sTimeUpper = "2017-04-25 14:16:30.000"; string sTimeLower = "2017-04-25 14:15:30.000"; DateTime TimeUpper = DateTime.Parse(sTimeUpper); DateTime TimeLower = DateTime.Parse(sTimeLower); Console.WriteLine($"TimeLower = {TimeLower}"); Console.WriteLine($"TimeUpper = {TimeUpper}"); List<string> res = (File.ReadAllLines(@"TimeStamp.txt") .Skip(1) .Select(line => line.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) .Where(fields => DateTime.Parse(fields[3]) > TimeLower && DateTime.Parse(fields[3]) < TimeUpper) .Select(fields => string.Join(",", fields)) .ToList<string>()); Console.WriteLine("Requested readings:"); foreach (string item in res) { Console.WriteLine(item); } 的两个地址是相同的。

0 个答案:

没有答案