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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:40:28+00:00 2026-06-19T04:40:28+00:00

Is it possible to write a C++ template owner_of<…> such that given this code:

  • 0

Is it possible to write a C++ template owner_of<...> such that given this code:

struct X { int y; }

owner_of<&X::y>::type is X?

  • 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-19T04:40:29+00:00Added an answer on June 19, 2026 at 4:40 am

    You can almost do that (or at least I could not find a better solution so far):

    #include <string>
    #include <type_traits>
    
    using namespace std;
    
    template<typename T>
    struct owner_of { };
    
    template<typename T, typename C>
    struct owner_of<T (C::*)>
    {
        typedef C type;
    };
    
    struct X
    {
        int x;
    };
    
    int main(void)
    {
        typedef owner_of<decltype(&X::x)>::type should_be_X;
        static_assert(is_same<should_be_X, X>::value, "Error" );
    }
    

    If you mind the use of decltype, maybe a macro could do:

    #define OWNER_OF(p) owner_of<decltype( p )>::type
    
    int main(void)
    {
        typedef OWNER_OF(&X::x) should_be_X;
        static_assert(is_same<should_be_X, X>::value, "Error" );
    }
    

    An alternative solution based on decltype:

    template<typename T, typename C>
    auto owner(T (C::*p)) -> typename owner_of<decltype(p)>::type { }
    
    int main(void)
    {
        typedef decltype(owner(&X::x)) should_be_X;
        static_assert(is_same<should_be_X, X>::value, "Error" );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to write template definition for this data type? myclass<int, myclass<int> >
Is it possible to write a template Foo<int n> such that: Foo<2> gives switch(x)
I can write a code. In this I can take a Template Column &
Is it possible to use reflection when we write a code-template? I was just
Is it possible to write code template or a snippet which will do following:
is it possible to write a partial template specialization that is used only for
Is it possible to write a C++(0x) metafunction that determines whether a type is
Is it possible to implement a class template in such a way that one
Is it possible to write an impure template in C++? That is, a template
Is it possible to write a class: template<typename T, ... name> struct Magic {

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.