public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Игорь Горлов" <gorlov.igorek@yandex.ru>
To: gcc-help@gcc.gnu.org
Subject: Invalid use of 'void'
Date: Mon, 18 May 2020 13:30:22 +0300	[thread overview]
Message-ID: <12029271589797822@iva7-8a22bc446c12.qloud-c.yandex.net> (raw)

Hi all.

I have an issue when compiling a simple console program on Windows (Msys2).
First, let's consider the program itself:

void.cpp:
#include <cstddef>
#include <cstdlib>
#include <iostream>
int main()
{
// Getting any 2 valid void pointers
void* p1=std::malloc(32);
void* p2=std::malloc(64);
if(p1==nullptr||p2==nullptr)
{
std::cout << "Error: failed to get 2 valid void pointers" << std::endl;
return 1;
}
// Now trying to calculate the difference between these pointers
std::ptrdiff_t difference=p2-p1;
std::cout << "The difference is " << difference << std::endl;
return 0;
}

This program is not compatible with the C++17 standard. However, we can compile it with -std=gnu++17 (C++17 with GNU extensions), because one of that extensions permits calculations on void pointers. Additionally, we append -Wno-pointer-arith to suppress the related warning. So, our command line looks like this:

# g++ void.cpp -std=gnu++17 -Wno-pointer-arith -o void.exe

GCC responds:
void.cpp: In function 'int main()':
void.cpp:16:30: error: invalid use of 'void'
   16 | std::ptrdiff_t difference=p2-p1;
      |                              ^~

What am I doing wrong?

             reply	other threads:[~2020-05-18 10:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-18 10:30 Игорь Горлов [this message]
2020-05-18 15:03 ` Niklas Gürtler
2020-05-18 20:03   ` Jonathan Wakely
2020-05-19 11:07     ` Florian Weimer
2020-05-19 11:31       ` Jonathan Wakely
2020-05-19 11:24 Игорь Горлов

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12029271589797822@iva7-8a22bc446c12.qloud-c.yandex.net \
    --to=gorlov.igorek@yandex.ru \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).