Sunday, September 25, 2016

volatile, reinterpre_case, constexpr, lambda and destructuring in C++14 and 17


#include "stdafx.h"
#include <iostream>

using namespace std;

namespace {
 volatile uint8_t &memory(const uint16_t loc)
 {
  return *reinterpret_cast<uint8_t*>(loc);
 }

 struct VIC_II
 {
  static constexpr uint16_t COLOR = 0xd020;
  volatile uint8_t &border()
  {
   return memory(COLOR);
  }
  volatile uint8_t& display(const uint8_t col, const uint8_t row)
  {
   return memory(1024 + col + row * 40);
  }


  auto test()
  {
   return std::make_tuple<int,int,int>(1, 2, 3);
  }
 };
}
int main()
{
 const auto le = [](uint8_t i) {cout << i << endl; };

 le(3);
 VIC_II vic;
 vic.border() = 1;
 vic.border() = 2;
 memory(1024) = 'a';
 vic.display(1, 2) = 'b';
 
// destructuring inside if does not work in VS 2015
// i.e. structured binding + if initializer
 if ( auto [s1, s2, s3] = vic.test();s1==1)
 {

 }
    return 0;
}

No comments:

Post a Comment