Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 933629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:51:54+00:00 2026-05-15T20:51:54+00:00

class Example { boost::shared_ptr<FilterProvider> filterProvider; public: void RegisterFilter(const boost::shared_ptr<FilterProvider>& toRegister) { filterProvider = toRegister;

  • 0
class Example
{
    boost::shared_ptr<FilterProvider> filterProvider;
public:
    void RegisterFilter(const boost::shared_ptr<FilterProvider>& toRegister)
    {
        filterProvider = toRegister;
    }
    const boost::shared_ptr<const FilterProvider>& GetFilter() const
    {
        return filterProvider; // Compiler reports "Returning address of local
                               // variable or temporary"
    }
};

I don’t see what’s local or temporary about filterProvider here; I’m returning what looks like a member variable of the class, not a temporary. (If I was actually returning a local variable or something like that the warning would make sense)

The specific warning is:

warning C4172: returning address of local variable or temporary.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:51:55+00:00Added an answer on May 15, 2026 at 8:51 pm

    Your shared ptr is declared with type

    boost::shared_ptr<FilterProvider>
    

    You are returning

    boost::shared_ptr<const FilterProvider>
    

    by const reference. See the difference?

    The types are not the same, but the former is convertible to the latter, and the compiler invokes the conversion. The result of the conversion is not an lvalue, but rather a temporary object, meaning that you are returning a const reference bound to a temporary object. This is legal initialization-wise, but the temporary will be destroyed right before the function returns. So in the calling code the reference will be invalid, which is what the compiler is warning you about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this example code: #include template<class T> class Class { public: typedef boost::shared_ptr<Class<T>
struct GPattern() { int gid; .... } class Example() { public: void run(string _filename,
From an example [1] in the documentation for boost::iterator_facade: class node_iterator : public boost::iterator_facade<
Consider the following example: #include Python.h #include <boost/python.hpp> #include <boost/shared_ptr.hpp> class A {}; class
Consider the following: class Example : boost::noncopyable { HANDLE hExample; public: Example() { hExample
Please consider the following simplified example: #include boost/shared_ptr.hpp #include boost/smart_ptr/enable_shared_from_this.hpp using namespace boost; class
For example, class Segment { friend bool someFunc( P_Segment p ); }; typedef boost::shared_ptr<Segment>
I came across this example class http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/sp_techniques.html#as_lock and I'm struggling with the syntax. class
Given: class example { public: std::vector<std::vector<int>> a; int b; } func() { example e;
In the following class: public class Example<T> where T : IComparable { private T

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.