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 3402912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:11:45+00:00 2026-05-18T05:11:45+00:00

I am faced with the following issue. Consider the following class: //Will be similar

  • 0

I am faced with the following issue. Consider the following class:

//Will be similar to bost::reference_wrapper
template<class T>
class Ref {
public:
  explicit Ref(T& t) : m_ptr(&t) {}
private:
  T* m_ptr;
};

and this function returning a double

double fun() {return 1.0;}

If we now have

double x = 1.0;
const double xc = 1.0;

Ref<double> ref1(x); //OK
Ref<const double> refc1(cx); //OK

good so far, however:

//Ref<double> ref2( fun() ); //Fails as I want it to
Ref<const double> refc2( fun() ); //Works but I would like it not to

Is there a way to modify Ref (the way you prefer) but not the function fun, so that the last line returns a compile-time error?
Please notice you can modify the constructor signature (as long as I am able to initialise the Ref as intended).

  • 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-18T05:11:46+00:00Added an answer on May 18, 2026 at 5:11 am

    You might not like the syntax to use it, but make the constructor take a pointer instead of a reference. You can’t take even a const pointer to a temporary.

    At least, not without smuggling it through another wrapper that hopefully makes the code Obviously Wrong(TM): template <typename T> T *reftoptr(T &t) { return &t; }

    That said, if you’re using this the way reference_wrapper is used, your users may actually want to capture temporaries. As long as the Ref object is also a temporary in the same full-expression as the temporary it captures, I think it’s OK. So for example,

    some_algorithm(iterator, anotherit, static_cast<Ref<const double> >(fun()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've faced with following issue, basically I do it like this: class Account(models.Model): TraffPerMonth
I faced following issue with my work: having two projects: MFC .dll and MFC
I faced the following issue while I submitting my form using jQuery FORM and
I am faced with the following issue and at this point I feel like
I'm faced with the following and I'm not sure what's best practice. Consider the
I am facing a similar issue to the problem faced by the similar StackOverflow
While porting Qt project from windows to linux(ubuntu) i faced with the following issue:
I am faced with the following issue. I need to capture a range of
I have the following website: http://miraj.quranprojectreading.org/ The issue that I'm faced with is that
I faced following issue: I need to check is the paired device via Bluetooth

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.