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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:07:40+00:00 2026-06-03T02:07:40+00:00

The following code #include <cassert> #include <cstddef> template <typename T> struct foo { foo(std::nullptr_t)

  • 0

The following code

#include <cassert>
#include <cstddef>

template <typename T>
struct foo {
    foo(std::nullptr_t) { }
    //friend bool operator ==(foo lhs, foo rhs) { return true; }

    template <typename U>
    friend bool operator ==(foo<U> lhs, foo<U> rhs);
};

template <typename T>
inline bool operator ==(foo<T> lhs, foo<T> rhs) { return true; }

int main() {
    foo<int> p = nullptr;
    assert(p == nullptr);
}

fails to compile with the error message

foo.cpp:18:5: error: no match for ‘operator==‘ in ‘p == nullptr‘
foo.cpp:18:5: note: candidate is:
foo.cpp:14:13: note: template<class T> bool operator==(foo<T>, foo<T>)
foo.cpp:14:13: note: template argument deduction/substitution failed:
foo.cpp:18:5: note: mismatched types ‘foo<T>‘ and ‘std::nullptr_t‘

However, if I use the definition inside the class instead, the code works as expected.

Let me say that I understand the error message: the template argument T cannot be deduced for the type of nullptr (incidentally, decltype(*nullptr) doesn’t compile). Furthermore, this can be fixed here by just defining the function inside the class.

However, for reasons of uniformity (there are other friend functions which I need to define outside) I would like to define this function outside of the class.

Is there a “trick” to make an outside of class definition of the function work?

  • 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-03T02:07:42+00:00Added an answer on June 3, 2026 at 2:07 am

    Abhijit has already given you the basic solution, but I thought I would expound upon it a bit since this is an interesting problem.

    If you declare a friend function inside a template class, like this:

    template <typename T>
    struct A {
      friend void f(A);
    };
    

    Then what you are saying is that any non-template function called f that takes A as a parameter will be a friend of A. So you would need to define these functions separately:

    inline void f(A<int>) {...}
    inline void f(A<float>) {...}
    // etc.
    

    Although defining it inside the class is a shortcut.

    In this case, there is no way to make a template that defines the friend f(A) for every T, because you’ve already stated that it is the non-template function that is the friend. It is the very fact that it is a non-template function that makes it usable in your example, because non-template functions allow more conversions than template functions when the compiler looks for matching functions.

    There is a fairly general workaround, although it is a bit messy. You can define other template functions that will handle your nullptr, or whatever else you might throw at it, and just defer it to your main function:

    template <typename T>
    inline bool operator ==(foo<T> lhs, std::nullptr_t rhs)
    {
      return lhs==foo<T>(rhs);
    }
    

    You may want to do it both ways for symmetry:

    template <typename T>
    inline bool operator ==(std::nullptr_t lhs,foo<T> rhs)
    {
      return foo<T>(lhs)==rhs;
    }
    

    On a separate note, the way you’ve defined your friend function makes operator==(foo<U>,foo<U>) a friend of foo<T> even when U and T are not the same types. It probably isn’t going to make much difference in practice, but there is a technically better way to do this. It involves forward declaring the template function, and then making the specialization for your template parameter be a friend.

    Here is a full example:

    template <typename> struct foo;
    
    template <typename T>
    inline bool operator==(foo<T> lhs,foo<T> rhs);
    
    template <typename T>
    struct foo {
        foo(std::nullptr_t) { }
    
        friend bool operator==<>(foo lhs,foo rhs);
    };
    
    template <typename T>
    inline bool operator ==(foo<T> lhs,foo<T> rhs)
    {
      return true;
    }
    
    template <typename T>
    inline bool operator ==(foo<T> lhs, std::nullptr_t rhs)
    {
      return lhs==foo<T>(rhs);
    }
    
    template <typename T>
    inline bool operator ==(std::null_ptr_t lhs,foo<T> rhs)
    {
      return foo<T>(lhs)==rhs;
    }
    
    int main() {
        foo<int> p = nullptr;
        assert(p == nullptr);
        assert(nullptr == p);
        assert(p == p);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
The following code #include <stdio.h> template <typename T, T v> class Tem { T
In the following code: #include <iostream> template <typename T, size_t N> void cal_size(T (&a)[N])
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
I have the following code: #include <cstdio> template<class Fun, class... Args> void foo(Fun f,
Assume the following code: #include <string> #include <iostream> using namespace std; struct A {
Having the following code: #include <iostream> struct A { int x; A(){} ~A(){std::cout <<~A(<<x<<)\n;}
Background Consider the following code: #include <utility> namespace ns { struct foo { foo()
I have the following code: #include <iostream> using namespace std; struct S { S(int
I have the following code: #include <memory> class Foo; typedef std::tr1::shared_ptr<Foo> pFoo_t; class DoSomething

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.