Skip to main content
deleted 23 characters in body
Source Link
Kushal
  • 8.5k
  • 10
  • 66
  • 83

In the following code, I'm taking address to static constexpr member:

 struct component_type_data{};  template<class Derived>  class component{ private:   const constexpr static component_type_data type_data{}; public:   static constexpr const component_type_data* component_type = &type_data;  }; 

My motivation is to have compile time unique id for type.

Is this valid? Code compiles only starting from C++17. And I can use that pointer as template argument.

If this valid, how compiler can know address beforehand?

UPDATE. :

Also, what happens across dll boundaries? Each dll will have its own unique address for the same static member, or they will be the same?

In the following code, I'm taking address to static constexpr member:

 struct component_type_data{};  template<class Derived>  class component{ private:   const constexpr static component_type_data type_data{}; public:   static constexpr const component_type_data* component_type = &type_data;  }; 

My motivation is to have compile time unique id for type.

Is this valid? Code compiles only starting from C++17. And I can use that pointer as template argument.

If this valid, how compiler can know address beforehand?

UPDATE.

Also, what happens across dll boundaries? Each dll will have its own unique address for the same static member, or they will be the same?

In the following code, I'm taking address to static constexpr member:

struct component_type_data{}; template<class Derived> class component{ private: const constexpr static component_type_data type_data{}; public: static constexpr const component_type_data* component_type = &type_data; }; 

My motivation is to have compile time unique id for type.

Is this valid? Code compiles only starting from C++17. And I can use that pointer as template argument.

If this valid, how compiler can know address beforehand?

UPDATE :

Also, what happens across dll boundaries? Each dll will have its own unique address for the same static member, or they will be the same?

added 153 characters in body
Source Link
tower120
  • 5.3k
  • 8
  • 49
  • 91

In the following code, I'm taking address to static constexpr member:

 struct component_type_data{}; template<class Derived> class component{ private: const constexpr static component_type_data type_data{}; public: static constexpr const component_type_data* component_type = &type_data; }; 

My motivation is to have compile time unique id for type.

Is this valid? Code compiles only starting from C++17. And I can use that pointer as template argument.

If this valid, how compiler can know address beforehand?

UPDATE.

Also, what happens across dll boundaries? Each dll will have its own unique address for the same static member, or they will be the same?

In the following code, I'm taking address to static constexpr member:

 struct component_type_data{}; template<class Derived> class component{ private: const constexpr static component_type_data type_data{}; public: static constexpr const component_type_data* component_type = &type_data; }; 

My motivation is to have compile time unique id for type.

Is this valid? Code compiles only starting from C++17. And I can use that pointer as template argument.

If this valid, how compiler can know address beforehand?

In the following code, I'm taking address to static constexpr member:

 struct component_type_data{}; template<class Derived> class component{ private: const constexpr static component_type_data type_data{}; public: static constexpr const component_type_data* component_type = &type_data; }; 

My motivation is to have compile time unique id for type.

Is this valid? Code compiles only starting from C++17. And I can use that pointer as template argument.

If this valid, how compiler can know address beforehand?

UPDATE.

Also, what happens across dll boundaries? Each dll will have its own unique address for the same static member, or they will be the same?

Source Link
tower120
  • 5.3k
  • 8
  • 49
  • 91

constexpr pointer to constexpr static member in C++17

In the following code, I'm taking address to static constexpr member:

 struct component_type_data{}; template<class Derived> class component{ private: const constexpr static component_type_data type_data{}; public: static constexpr const component_type_data* component_type = &type_data; }; 

My motivation is to have compile time unique id for type.

Is this valid? Code compiles only starting from C++17. And I can use that pointer as template argument.

If this valid, how compiler can know address beforehand?