Accepted answer
since you're not in control of the return type, you must make sure you return a valid object and not just a temporary. one solution would be a function-local static variable:
virtual const core::matrix4& getviewmatrixaffector() const
static const core::matrix4 val;
return val;
if you find yourself doing this in many functions (with the same type of the variable), make val
a (suitably renamed) static member of the class.
intel c++ 14.0.3.202 gives this warning even if the reference is valid outside the function. this is just a bug i witnessed in this version, but it may also appear in others. if you use this version: just mask the warning out by wrapping your function this way:
#pragma warning(push)
#pragma warning(disable:473)
( your function definition )
#pragma warning(pop)
i´m not sure if 473 is the index of this warning - but you see the correct one in the compuler´s messages.
when you return by reference, as in core::matrix4&
, you need an object which will still be around when the function returns. in your case, you are returning a "local temporary object", which is destructed after that function exits. in order to fix this, you need to return by value, like so:
virtual const core::matrix4 getviewmatrixaffector() const {return core::matrix4();};
// ^ no '&'
when you create an object as a local temporary, it is destroyed as soon as the function's scope ends. in turn, you should never return a reference to it, as this would yield undefined behaviour.
consider returning it by value, or returning a smart pointer to an object on the free store.
returning reference to local temporary object on pointer dereferencing
Returning reference to local temporary object
Returning temporary object and binding to const reference
Returning a reference to a local or temporary variable
Is returning a reference to a local object undefined behavior in copy initialization?
Why am I getting "warning: returning reference to local temporary object"?
Returning reference of a temporary object from a function
Returning const reference to temporary behaves differently than local const reference?
returning const reference to a member of a temporary object
Compiler warning when returning a reference to a local object
C++ reference to local temporary object
C++ Returning reference to local variable
Returning a const reference to an object instead of a copy
Why doesn't a const reference extend the life of a temporary object passed via a function?
Returning const reference to local variable from a function
C++ best practice: Returning reference vs. object
Why is returning a reference to a function local value not a compile error?
warning: returning reference to temporary
Why do compilers give a warning about returning a reference to a local stack variable if it is undefined behaviour?
Can't pass temporary object as reference
Why is returning address of local variable or temporary only a warning and not an error?
const reference to member of temporary object
Why is move constructor not picked when returning a local object of type derived from the function's return type?
returning address or local variable or temporary C++ warning
Does returning a local object require move semantics?
C++ Returning reference to temporary
Extending the life of a temporary object by getting a reference to a subobject
Returning a const reference to vector of an object
return const reference vs temporary object
Returning a local object from a function
More Query from same tag
C++ - Identifying a family of polymorphic classes without introducing tight coupling
Using a namespace in place of a static class in C++?
Fragmented MP4 - problem playing in browser
Dynamically Allocated Array of a Class
Is there a C++ equivalent to python's functools.partial
C++ - segmentation fault reading binary file
How to limit emitted vertices with Geometry shader using Transform feedback?
Function definition not found for a function declared inside unnamed namespace - how to resolve? (Visual Studio 2015)
How does QImage with Format_Mono store informations ?
Unexpected result from shift right g++
looking for a stand-alone, in-memory data server with sequential access
invalid conversion from 'char' to 'char*' using strcpy
QSqlDatabase and connecting to .sqlite file
C++: Having unicode console title..?
Results of tbb::parallel_reduce and std::accumulate differ
How to convert string to const unsigned char* without using reinterpret_cast (modern approach)
Visual Studio Debug Runtime Errors for OCCI C++ Application
How can I parse a bracketed string to a list of string with a given delimitor
syntax for declaring a function template
Cartesian product for multiple sets at compile time
Assign single dimension array as an element of two(multi) dimensional array in (C/C++)
Creating Bitmap image from OpenGL ES Framebuffer through JNI on Android NDK
Setting and using path to data directory with GNU AutoTools
Strange behavior when a function declared as noexcept throws an exception in its default argument
C++ stdlib container class hierarchy
Determine difference in stops between images with no EXIF data
Can I reliably set a function pointer to NULL in C and C++?
Qt Map Signals Based On Parameter Value
Sparse array in C++
is it possible to use QtConcurrent::run() with a function member of a class