Skip to content

Commit b4c4aa5

Browse files
committed
GCC/Clang: Turn -Wdangling-reference into error, if available
you usually get that when calling a method that returns a reference on a temporary and assigning that to a reference variable. Often nasty do debug... Example: idVec3 v1 = ...; idVec3 v2 = ...; const idVec2& cross2d = v1.Cross(v2).ToVec2(); idVec3::Cross(const idVec3 &a) returns idVec3, i.e. a temporary, and idVec3::ToVec2() returns a reference to the idVec2 part of an idVec3. That temporary is gone after that line and cross2d points to invalid memory.. (I found this change in another branch, don't remember what triggered me creating it, might even have been a bug in another project, but either way it's great that compilers can now warn about this and if they do that should be treated as an error and be resolved.)
1 parent 88f7b8c commit b4c4aa5

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

neo/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ if(D3_COMPILER_IS_GCC_OR_CLANG)
417417
add_compile_options(-Wno-sign-compare)
418418
add_compile_options(-Wno-switch)
419419

420+
CHECK_CXX_COMPILER_FLAG("-Wdangling-reference" cxx_has_Wdangling_reference)
421+
if(cxx_has_Wdangling_reference)
422+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=dangling-reference")
423+
endif()
424+
420425
CHECK_CXX_COMPILER_FLAG("-Woverloaded-virtual" cxx_has_Woverload_virtual)
421426
if(cxx_has_Woverload_virtual)
422427
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual")

0 commit comments

Comments
 (0)