Recent Posts

Pages: 1 [2] 3 4 ... 10
11
Computers & Programming General / Structured Bindings
« Last post by Hooman on December 04, 2018, 03:52:19 AM »
It seems JavaScript and Ruby like destructuring has come to C++17 as Structured Bindings. This provides shorthand notation to pull out elements of arrays, tuples, and structs, into new variable declarations.

Examples:
Code: cpp [Select]

#include <iostream>
#include <tuple>

// Helper method
void printPairs(int v1, int v2) {
  std::cout << "{" << v1 << ", " << v2 << "}" << std::endl;
}


void demoArrayStructuredBindings() {
  int a[2] = {1, 2}; // Declare and initialize array

  // Declare variables with structured binding
  auto [a0, a1] = a; // Copy array values
  auto& [ra0, ra1] = a; // Set reference to array values

  printPairs(a0, a1); // {1, 2}  (Print copies)
  printPairs(ra0, ra1); // {1, 2}  (Print references to originals)

  // Update original array
  a[0] = 2;
  a[1] = 3;

  printPairs(a0, a1); // {1, 2}  (Copied values are unaffected)
  printPairs(ra0, ra1); // {2, 3}  (References reflect updates)
}


void demoTupleStructuredBindings() {
  // Create tuple
  auto tuple = std::make_tuple(1, 4);

  // Declare variables using structured binding
  auto [v0, v1] = tuple;

  printPairs(v0, v1); // {1, 4}
}


void demoStructStructuredBindings() {
  struct S {
    int field1:4; // Bitfield's work too
    int field2;
  };

  // Create a struct
  S s{5, 6};

  // Declare variables with structured binding
  auto [s0, s1] = s;

  printPairs(s0, s1); // {5, 6}
}


int main() {
  demoArrayStructuredBindings();
  demoTupleStructuredBindings();
  demoStructStructuredBindings();

  return 0;
}


The case of the bitfield reference is particularly interesting. When working with GCC, I see warnings if I try to set the reference value beyond what the bitfield can hold. That suggests it's able to keep track of the restricted range of the bitfield.
Code: cpp [Select]

  S s1{0, 16};
  auto& [s1f1, s1f2] = s1;
  s1f1 = 16;  // Warning: Overflow


If I had instead tried to set a normal reference, it would have failed:
Code: cpp [Select]

  auto& r = s1.field1; // Error: Cannot bind bitfield


The closest I've been able to do, is bind to a temporary copy of the value stored in the bitfield. This converts to the underlying type, and so the bitfield restriction is lost. If the binding is an rvalue reference, you can modify the temporary. If you use an lvalue reference, it must be declared const for the binding to be allowed. In either case, the binding is to a temporary copy, and so changes to the original field are not reflected:
Code: cpp [Select]

int&& r1 = s1.field1; // Ok, read s1.field into a temporary of type int (no bitfield restriction), and set r1 to point to the temporary
const int& r2 = s1.field1; // Ok, read s1.field into a temporary of type int (no bitfield restriction), and set r2 to point to the temporary (read only)

12
Computers & Programming General / Sizeof versus sizeof...
« Last post by Hooman on December 04, 2018, 12:22:14 AM »
Recently I've been noticing "sizeof..." used with parameter packs in templated code. For example:
Code: cpp [Select]

template< class... Types >
class tuple_size< std::tuple<Types...> >
 : public std::integral_constant<std::size_t, sizeof...(Types)> { };


The syntax struck me as a bit odd, and I wasn't sure how to parse it. I suppose I was thinking of the "..." as some kind of operator that was being applied to the usual sizeof. Turns out that no, it's something different.

sizeof - Returns the byte size of a variable/expression or type
sizeof... - Returns the number of items in a parameter pack

It's special syntax that means something very specific. Even the parenthesis seem to be part of the syntax. If you try to omit the parenthesis, it's a compile error. With GCC, you can reduce the error to a warning with the flag "-fpermissive", but it still flags the code as bad.

The treatment of the parenthesis seemed in contrast to the usual sizeof operator, where parenthesis are commonly used, though optional. Or so I thought. It turns out the parenthesis for regular sizeof are only optional for expressions, but required for types.
Code: cpp [Select]

  // Use with variable/expression
  int array[1];
  std::cout << sizeof(array) << std::endl; // Ok: 4
  std::cout << sizeof array  << std::endl; // Ok: 4  (parenthesis optional)

  // Use with type
  std::cout << sizeof(int[1]) << std::endl; // Ok: 4
  std::cout << sizeof int[1]  << std::endl; // Syntax error


I should also emphasise that regular sizeof returns the byte size, not the number of elements in an array. This is in constrast to sizeof..., which returns the number of items in a parameter pack. It seems they are reusing the sizeof keyword, for two concepts that aren't all that closely related, much like how auto got repurposed.

Here's an example using parameter packs and sizeof...:
Code: cpp [Select]

#include <iostream>

template<typename... Types>
void printNumParams(Types... values) {
  std::cout << sizeof...(Types) << std::endl; // Ok
  //std::cout << sizeof... Types  << std::endl; // Syntax Error/Warning
}

int main() {
  printNumParams(1, 3.14); // 2
  printNumParams(1, 3.14, "string"); // 3
}

13
Feedback/Suggestions/Problems / Re: SVN Certificate Issue
« Last post by Hooman on December 03, 2018, 11:56:16 PM »
My git-svn copy is up to date. Actually, it was up to date all along, I just didn't realize it because I checked history of the local master branch, rather than the remote git-svn tracking branch.

My native SVN clone is still out of date. I couldn't remember the update command at the time the server was first back up. Mind you, I could "git svn dcommit" the git-svn clone to a deployed copy of the SVN clone. That would bring it up to date in native SVN format. It might change a bit of meta data though, but all the file changes will be preserved.

In short, our backups aren't in too bad of a shape right now.
14
This is the 24th Alpha Release. Main page links updated.

-> Added three powerful artifacts, that remain best in slot at all times, scaling in power as you go deeper into the game.
-> Added the Trials menu (Shift + 4), and 6 Trials. Completing a Trial, ends the game in a Victory, doing everything that they properly need to do so. The ending message is a bit bare bones at the moment. Saving Shift + 3 for the Milestones menu, which will allow for unlocking Legendary Runes (which didn't make it in this release).
-> Fixed a bunch of nasty crash bugs, that delayed the release for 2 days. Everything should be running smoothly now. I hope.
-> Moving closer to the Beta release now!
-> Got roughly (albeit in most cases bare bone frameworks) 70-80% of my monthly November goals accomplished. This week I'll try to get my Legendary Runes to work (so that I can attach some to my Artifacts) and start working on Map Generation improvements.
15
Feedback/Suggestions/Problems / Re: SVN Certificate Issue
« Last post by Vagabond on December 03, 2018, 04:33:44 PM »
The Virtual Server now comes up for me but gives an error after a few minutes. So some part of the server still seems to be working, but the data isn't accessible. Did you manage to clone it before it went down again? I did not.

-Brett
16
Feedback/Suggestions/Problems / Re: SVN Certificate Issue
« Last post by Hooman on December 03, 2018, 05:31:23 AM »
Is it just me, or is it down again?
17
Outpost 2 Update / Re: Updates for Outpost 2 1.3.8
« Last post by Hooman on December 02, 2018, 01:26:16 PM »
Let me knows when the new package is ready and tested.
18
Feedback/Suggestions/Problems / Re: SVN Certificate Issue
« Last post by Hooman on December 02, 2018, 01:24:07 PM »
"Yeah!" and there was much rejoicing. Though we were forced to eat Sir Leviathan's minstrels. :P
19
Feedback/Suggestions/Problems / Re: SVN Certificate Issue
« Last post by Vagabond on December 02, 2018, 05:09:55 AM »
The SVN server is restored with a valid certificate. Thanks Leviathan! Ok, no excuse for me anymore to not work on getting a new release of Outpost 2 ready. :)

-Brett
20
Game Discussion General / Re: Empires of Eradia: The Cataclysm of Chaos - Alpha V23
« Last post by Vagabond on December 02, 2018, 05:08:48 AM »
That's pretty clever. Nice Easter egg for a player to stumble on I think.

-Brett
Pages: 1 [2] 3 4 ... 10