0

The code below was copied from "The GNU C Library" here

In this question @Fatih suggested a change for the the code to compile, as I show in a comment below.

#include <signal.h>
#include <stdio.h>
#include <unistd.h>    // This was inserted by Fatih 

volatile struct two_words { int a, b; } memory;

void
handler(int signum)
{
   printf ("%d,%d\n", memory.a, memory.b);
   alarm (1);
}

int
main (void)
{
   static struct two_words zeros = { 0, 0 }, ones = { 1, 1 };
   signal (SIGALRM, handler);
   memory = zeros;
   alarm (1);
   while (1)
     {
       memory = zeros;
       memory = ones;
     }
}

But the code still doesn't compile, as can be seen here. Error messages:

main.cpp: In function 'int main()':
main.cpp:19:13: error: passing 'volatile two_words' as 'this' argument discards qualifiers [-fpermissive]
   19 |    memory = zeros;
      |             ^~~~~
main.cpp:5:17: note:   in call to 'constexpr two_words& two_words::operator=(const two_words&)'
    5 | volatile struct two_words { int a, b; } memory;
      |                 ^~~~~~~~~
main.cpp:23:17: error: passing 'volatile two_words' as 'this' argument discards qualifiers [-fpermissive]
   23 |        memory = zeros;
      |                 ^~~~~
main.cpp:5:17: note:   in call to 'constexpr two_words& two_words::operator=(const two_words&)'
    5 | volatile struct two_words { int a, b; } memory;
      |                 ^~~~~~~~~
main.cpp:24:17: error: passing 'volatile two_words' as 'this' argument discards qualifiers [-fpermissive]
   24 |        memory = ones;
      |                 ^~~~
main.cpp:5:17: note:   in call to 'constexpr two_words& two_words::operator=(const two_words&)'
    5 | volatile struct two_words { int a, b; } memory;
      |                 ^~~~~~~~~

What am I missing now?

Daniel
  • 19
  • 1
  • You added a `volatile` which makes the code different vs the original example. – πάντα ῥεῖ Nov 22 '20 at 13:38
  • @πάνταῥεῖ The `volatile` is in the original code in GNU. As I said the code still doesn't compile. Any suggestion? – Daniel Nov 22 '20 at 13:48
  • 2
    As you've noted yourself this code is from the The GNU **C** Library (emphasis on **C**). The question to which you [refer](https://stackoverflow.com/q/52859599/8405691) is also tagged `C` rather than `C++` and the code does indeed compile when [compiled as `C`](https://godbolt.org/z/sa6G9M). – G.M. Nov 22 '20 at 14:29
  • 1
    [This question](https://stackoverflow.com/questions/49367852/volatile-struct-struct-not-possible-why) and [this](https://stackoverflow.com/questions/17217300/why-am-i-not-provided-with-a-default-copy-constructor-from-a-volatile) should shed light on why this does not work in C++ – NotAProgrammer Nov 22 '20 at 14:39
  • @NotAProgrammer Two great links that answer my question. Thank you. – Daniel Nov 22 '20 at 17:26
  • @NotAProgrammer The notes above in the error messages seem to indicate that the default assignment operator `two_words& two_words::operator=(const two_words&)` is `constexpr`. Do you happen to know why this is so? – Daniel Nov 22 '20 at 18:45
  • @Daniel The implicitly-generated copy assigment operator is declared `constexpr` because it satisfies the requirements in [\[class.copy.assign\]\10](http://eel.is/c++draft/class.copy.assign#10). – NotAProgrammer Nov 22 '20 at 19:16
  • @NotAProgrammer Great answer. Thank you. – Daniel Nov 22 '20 at 21:28

0 Answers0