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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:47:42+00:00 2026-06-05T03:47:42+00:00

Like std::reference_wrapper uses a pointer under the covers to store a reference, I am

  • 0

Like std::reference_wrapper uses a pointer under the covers to store a “reference”, I am trying to do something similar with the following code.

#include <type_traits>

struct Foo
{
    void* _ptr;

    template<typename T>
    Foo(T val,
        typename std::enable_if
            <
                std::is_reference<T>::value,
                void
            >::type* = nullptr)
        : _ptr(&val)
    { }
};

int main()
{
    int i = 0;
    int& l = i;

    Foo u2(l);

    return 0;
}

However, this fails to compile:

CXX main.cpp
main.cpp: In function ‘int main()’:
main.cpp:23:13: error: no matching function for call to ‘Foo::Foo(int&)’
main.cpp:23:13: note: candidates are:
main.cpp:8:5: note: template<class T> Foo::Foo(T, typename std::enable_if<std::is_reference<_Tp>::value, void>::type*)
main.cpp:8:5: note:   template argument deduction/substitution failed:
main.cpp: In substitution of ‘template<class T> Foo::Foo(T, typename std::enable_if<std::is_reference<_Tp>::value, void>::type*) [with T = int]’:
main.cpp:23:13:   required from here
main.cpp:8:5: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
main.cpp:3:8: note: constexpr Foo::Foo(const Foo&)
main.cpp:3:8: note:   no known conversion for argument 1 from ‘int’ to ‘const Foo&’
main.cpp:3:8: note: constexpr Foo::Foo(Foo&&)
main.cpp:3:8: note:   no known conversion for argument 1 from ‘int’ to ‘Foo&&’

How can I make the enable_if return true for reference parameters?

  • 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-06-05T03:47:45+00:00Added an answer on June 5, 2026 at 3:47 am

    T in this case will never be deduced to be a reference type. In your construction of the object u2, the constructor template argument is deduced to be int.

    While the type of the variable u2 is int&, when you use u2 in an expression, it is an lvalue expression of type int. An expression never has reference type.

    Template argument deduction uses the types of the function arguments to deduce the template parameter types. Function arguments are expressions. Therefore, because no expression has reference type, a template argument will never be deduced to be a reference type.

    [In C++11, if a function argument has type T&&, T may be deduced to the type T& if the argument is an lvalue. This mechanism enables perfect forwarding. That’s not related to your scenario, though.]

    In effect, in an expression, an object and a reference to that object are indistinguishable. A reference just allows you to give another name to the object.

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

Sidebar

Related Questions

It looks like std::cout can't print member function's address, for example: #include <iostream> using
Say I've got a class where the sole data member is something like std::string
I have some code that looks like: static const std::string and( AND ); This
I've got a loop in my code that uses std::basic_string<HANDLE> , and then waits
For training, I'm trying to write a class like std::string. I have read that
I would like to store a reference to an object as a weak_ptr. In
I'm trying to create a class like std::cout , however, with colored output. The
I'm curious as to exactly how this feature works. Consider something like std::unique_ptr<int> f()
Suppose I have something like this: class Collection { private: typedef std::vector< std::shared_ptr<Something> >::iterator
Can any of STL algorithms/container operations like std::fill , std::transform be executed in parallel

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.