-1

I am trying to run this texture to pixmap example from OpenGL and get the following error

tex_to_pix.cpp:40:1: error: narrowing conversion of ‘4294967295u’ from ‘unsigned int’ to ‘int’ inside { } [-Wnarrowing]

The error refers to the following code block of the example:

const int pixmap_config[] = {
    GLX_BIND_TO_TEXTURE_RGBA_EXT, True,
    GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
    GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
    GLX_DOUBLEBUFFER, False,
    GLX_Y_INVERTED_EXT, GLX_DONT_CARE,
    None
};

What is the reason for this error?

Is it a compiler or c++11 problem?

Is there a way i can either make my compiler ignore -Wnarrowing or make a safe conversion?

ephtron
  • 1,115
  • 1
  • 10
  • 16
  • 1
    Instead of `const int pixmap_config[] ...`, try `const unsigned int pixmap_config[] ...` – Amadeus Oct 30 '17 at 16:01
  • @Amadeus: The function `glXChooseFBConfig(Display *dpy, int screen, const int *pixmap_config, int *nitems)` needs pixmap_config to be a `const int*`. `glXChooseFBConfig` gets used in line 158. – ephtron Oct 30 '17 at 16:13
  • 2
    Seems to me that it is a problem with the API. You could try to compile in c++03 or try to disable this warning (search in google for "disable narrowing warning g++" or whatever compiler you are using) – Amadeus Oct 30 '17 at 16:18

1 Answers1

2

The issue is with GLX_DONT_CARE which is defined as:

#define GLX_DONT_CARE                     0xFFFFFFFF

Because this value does not fit into a 32-bit int, its type is unsigned int (see this answer). The rules for narrowing conversion were indeed changed in c++11.

Trying to implicitly convert this unsigned int into an int causes the narrowing conversion warning. As shown in this answer, the narrowing problem can be fixed by using static_cast(GLX_DONT_CARE & 0xFFFFFFFF) instead of GLX_DONT_CARE

const int pixmap_config[] = {
    GLX_BIND_TO_TEXTURE_RGBA_EXT, True,
    GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
    GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
    GLX_DOUBLEBUFFER, False,
    GLX_Y_INVERTED_EXT, static_cast<int>(GLX_DONT_CARE & 0xFFFFFFFF),
    None
};

Alternatively, disable narrowing conversion errors in your compiler (unspecified).

ephtron
  • 1,115
  • 1
  • 10
  • 16
Rotem
  • 21,452
  • 6
  • 62
  • 109