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

  • SEARCH
  • Home
  • 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 7090941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:07:08+00:00 2026-05-28T08:07:08+00:00

I want to store a reference to an object in a boost::any object. How

  • 0

I want to store a reference to an object in a boost::any object. How do I initialize the boost::any object? I tried std::ref(), but boost::any gets initialized with std::reference_wrapper<>. For example, the following

#include <boost/any.hpp>
#include <cxxabi.h>
#include <iostream>

int main(void)
{
  int s;
  int i = 0;
  boost::any x(std::ref(i));
  std::cout << abi::__cxa_demangle(x.type().name(), 0, 0, &s) << "\n";
  return 0;
}

prints

std::reference_wrapper<int>

I want the boost::any to contain int& instead.

  • 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-28T08:07:09+00:00Added an answer on May 28, 2026 at 8:07 am

    The boost::any class doesn’t have an interface allowing something like this: you would need to specify the type of the reference with the constructor. I don’t think that you can explicitly specify the type of templated constructor because I don’t see any place you could stick it. Even if you can explicitly specify the template parameter, it wouldn’t work in C++2003 bcause there is no reference collapsing available and the parameter is declared as taking a T const&: you’d be trying to create a T& const& which won’t fly.
    I think your best option is to either use std::reference_wrapper<T> if you insist on something looking remotely reference like or just to use T*.

    That said, it would be generally possible to have a templatized static factor method of a type similar to boost::any which would be used to explicitly specify the template argument. However, since boost::any is deliberately designed to deal with value types this isn’t done. I’m a bit dubious whether it should be done as well: using a pointer is perfectly good alternative. If you really need a reference type you’ll probably have to implement it yourself.

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

Sidebar

Related Questions

I want to store the current URL in a session variable to reference the
I want to store a large number of sound files in a database, but
I'm using ptr_map for storing different types of pointers. boost::ptr_map<string, any> someMap; I store
I was wondering how one could store a reference to an object in .net.
I want to store a small list of a simple object (containing three strings)
I'm wondering if there's a way for Binding object to store reference to an
In Java, we can always use an array to store object reference. Then we
I want to store the bitmap data from _sampleTile in array, but I was
I am wondering how to store a reference of an object inside of another
I want to send an object through reference. The object represents a class [test.php]

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.