c++ - C++Niche Syntax: Function Reference Type Declaration: const reference? - Stack Overflow

admin2025-04-29  4

This is a niche question, but I'm struggling to find a sufficient answer.

Struct members can be const, but can a function pointer/reference member be declared const? Based off my reading of C++17 section 9.3, I don't think so:

struct Ex {
  const int i;
  void (*pfn)(int i); // 1. can pfn be a const member?
  void (&rfn)(int i); // 2. can rfn be a const member?
};

This is a niche question, but I'm struggling to find a sufficient answer.

Struct members can be const, but can a function pointer/reference member be declared const? Based off my reading of C++17 section 9.3, I don't think so:

struct Ex {
  const int i;
  void (*pfn)(int i); // 1. can pfn be a const member?
  void (&rfn)(int i); // 2. can rfn be a const member?
};
Share Improve this question edited Jan 7 at 3:42 Remy Lebeau 602k36 gold badges508 silver badges854 bronze badges asked Jan 6 at 22:02 JWCSJWCS 1,2211 gold badge9 silver badges22 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 5
  1. Yes:
    struct Ex {
        const int i;
        void (*const pfn)(int i); // 1. can pfn be a const member?
        void (&rfn)(int i);       // 2. can rfn be a const member?
    };
    
  2. It already is - You can't assign to a member referencing a function.
转载请注明原文地址:http://anycun.com/QandA/1745941928a91432.html