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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:57:51+00:00 2026-05-13T06:57:51+00:00

Consider the following code template<typename T, int N> struct A { typedef T value_type;

  • 0

Consider the following code

template<typename T, int N>
struct A {
  typedef T value_type; // OK. save T to value_type
  static const int size = N; // OK. save N to size
};

Look, it is possible to save any template parameter if this parameter is a typename or an integer value. The thing is that pointer to member is an offset, i.e. integer.
Now I want to save any pointer to member in compile time:

struct Foo {
   int m; 
   int r;
};

template<int Foo::*ptr_to_member>
struct B {
   // Next statement DOES NOT WORK!
   static int Foo::* const saved_ptr_to_member = ptr_to_member; 
};

// Example of using
int main() {
    typedef B<&Foo::m> Bm;
    typedef B<&Foo::r> Br;
    Foo foo;
    std::cout << (foo.*(Bm::saved_ptr_to_member));
}

How to save pointer to member in compile time? I use VS2008.

Note. Compile time is critical. Please don’t write run-time solution. I know it.

  • 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-13T06:57:51+00:00Added an answer on May 13, 2026 at 6:57 am

    Why using a template?

    #include <cstdio>
    
    struct Foo {
        int a;
        int b;
    } foo = {2, 3};
    
    int const (Foo::*mp) = &Foo::b;
    
    int
    main() {
        printf("%d\n", foo.*mp);
        return 0;
    }
    

    The following compiles mp to this on gcc-4.4.1 (I don’t have access to MSVC at the moment):

    .globl mp
            .align 4
            .type   mp, @object
            .size   mp, 4
    mp:
            .long   4
    

    It is just an offset to the member, which looks pretty compile-time to me.

    With template, you need to specify the definition outside of the class:

    #include <cstdio>
    
    struct Foo {
       int m;
       int r;
    } foo = {2, 3};
    
    template<int Foo::*Mem>
    struct B {
       static int Foo::* const mp;
    };
    
    template<int Foo::*Mem>
    int Foo::* const B<Mem>::mp = Mem;
    
    int main() {
        typedef B<&Foo::m> Bm;
        typedef B<&Foo::r> Br;
        printf("%d, %d\n", foo.*(Bm::mp), foo.*(Br::mp));
    }
    

    Which compiles to:

    g++ -O2 -S -o- b.cc | c++filt
    
    ...
    
            .weak   B<&(Foo::r)>::mp
            .section        .rodata._ZN1BIXadL_ZN3Foo1rEEEE2mpE,"aG",@progbits,B<&(Foo::r)>::mp,comdat
            .align 4
            .type   B<&(Foo::r)>::mp, @object
            .size   B<&(Foo::r)>::mp, 4
    B<&(Foo::r)>::mp:
            .long   4
            .weak   B<&(Foo::m)>::mp
            .section        .rodata._ZN1BIXadL_ZN3Foo1mEEEE2mpE,"aG",@progbits,B<&(Foo::m)>::mp,comdat
            .align 4
            .type   B<&(Foo::m)>::mp, @object
            .size   B<&(Foo::m)>::mp, 4
    B<&(Foo::m)>::mp:
            .zero   4
    

    However this all smacks of standard library features reimplementation (see std::tr1::mem_fn).

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

Sidebar

Ask A Question

Stats

  • Questions 248k
  • Answers 248k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To monitor multiple devices you will need a 3rd-party TAPI… May 13, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer First, it would be a mistake to rely on the… May 13, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer Use git filter-branch with a --parent-filter on G: $ git… May 13, 2026 at 8:53 am

Related Questions

( Preamble: I am a late follower to the C++0x game and the recent
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following piece of code: class B { private: // some data members
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following code template<unsigned int N> void foo(std::bitset<N> bs) { /* whatever */

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.