Home > Net >  declared ‘[[noreturn]]’ but its first declaration was not
declared ‘[[noreturn]]’ but its first declaration was not

Time:11-04

I recently learned about the [[noreturn]] attribute and wanted to try and implement it on one of my existing code snippets.

I added the attribute to a void return type function with no return; keyword on it whatsoever. However, I'm getting this error:

[ 17%] Building CXX object CMakeFiles/Renderer.dir/src/opengl/text_render.cpp.o
/home/turgut/Desktop/CppProjects/videoo-render/src/opengl/text_render.cpp:7:25: error: function ‘const void OpenGL::Text::set_background(int, int, float, int, int, int, int, float, std::string*)’ declared ‘[[noreturn]]’ but its first declaration was not
    7 | [[noreturn]] const void Text::set_background(
      |   

make[2]: *** [CMakeFiles/Renderer.dir/build.make:132: CMakeFiles/Renderer.dir/src/opengl/text_render.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:277: CMakeFiles/Renderer.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

I'm using c 20 so I don't think there is a problem with versions.

What is wrong with my usage of [[noreturn]] and how should I use it propperly? Am I missing a piece of knowledge about it?

Here is the function in question:

[[noreturn]] void Text::set_background(
    int x, int y, float z,
    int w, int h, 
    int gw, int gh,
    float ang, std::string* hex_color
){
    background = new Background(x-w, y-h);
    background->color = (*hex_color).c_str();

    background->bg_texture = new Texture(
        x - 10, (1000 - y)   10, w, -h ,gw, gh
    );

    background->bg_texture->init(ang, z, nullptr);
}

I've also tried to use it on some other similar functions but got a similar result.

CodePudding user response:

The attributes needs to be in the actual declaration of the function, the one you have in the header file inside the Text class.

For the definition (implementation) of the function you don't need to use the attribute again.


On a couple of different notes, using the const qualifier for a void return type makes no sense.

And the [[noreturn]] attribute is to tell the compiler that the function doesn't return at all. For example the std::exit function doesn't return and is thus marked with that attribute.

You function do return (I assume?) so the attribute makes no sense for that function.

CodePudding user response:

There were 2 issues about this error code, first is what Some programmer dude have posted. The other is that I forgot to add the same attribute to both header and the cpp file.

So it had to be put on both the implementation and the declaration.

  • Related