Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I am trying to make a compile-time string class. I took a few hints from this postthis post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str); } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; }; constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

There's lots of cool things I can do if I can make compile-time string constants.

  • string equality testing is faster on string than const char *
  • Eliminate run-time overhead of implicit conversions from const char * to string that call strlen() on compile-time constant strings.
  • Compile-time string sets that do equality testing instead of hashing for size < N. (this is multiple cpus of overhead on one application I'm looking at)

I am trying to make a compile-time string class. I took a few hints from this post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str); } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; }; constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

There's lots of cool things I can do if I can make compile-time string constants.

  • string equality testing is faster on string than const char *
  • Eliminate run-time overhead of implicit conversions from const char * to string that call strlen() on compile-time constant strings.
  • Compile-time string sets that do equality testing instead of hashing for size < N. (this is multiple cpus of overhead on one application I'm looking at)

I am trying to make a compile-time string class. I took a few hints from this post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str); } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; }; constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

There's lots of cool things I can do if I can make compile-time string constants.

  • string equality testing is faster on string than const char *
  • Eliminate run-time overhead of implicit conversions from const char * to string that call strlen() on compile-time constant strings.
  • Compile-time string sets that do equality testing instead of hashing for size < N. (this is multiple cpus of overhead on one application I'm looking at)
fixed missing semicolons on source code
Source Link
Slava
  • 44.4k
  • 2
  • 54
  • 100

I am trying to make a compile-time string class. I took a few hints from this post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str); } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; }; constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

There's lots of cool things I can do if I can make compile-time string constants.

  • string equality testing is faster on string than const char *
  • Eliminate run-time overhead of implicit conversions from const char * to string that call strlen() on compile-time constant strings.
  • Compile-time string sets that do equality testing instead of hashing for size < N. (this is multiple cpus of overhead on one application I'm looking at)

I am trying to make a compile-time string class. I took a few hints from this post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str) } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; } constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

There's lots of cool things I can do if I can make compile-time string constants.

  • string equality testing is faster on string than const char *
  • Eliminate run-time overhead of implicit conversions from const char * to string that call strlen() on compile-time constant strings.
  • Compile-time string sets that do equality testing instead of hashing for size < N. (this is multiple cpus of overhead on one application I'm looking at)

I am trying to make a compile-time string class. I took a few hints from this post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str); } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; }; constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

There's lots of cool things I can do if I can make compile-time string constants.

  • string equality testing is faster on string than const char *
  • Eliminate run-time overhead of implicit conversions from const char * to string that call strlen() on compile-time constant strings.
  • Compile-time string sets that do equality testing instead of hashing for size < N. (this is multiple cpus of overhead on one application I'm looking at)
added 455 characters in body
Source Link
Dave Dopson
  • 42.9k
  • 21
  • 97
  • 85

I am trying to make a compile-time string class. I took a few hints from this post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str) } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; } constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

There's lots of cool things I can do if I can make compile-time string constants.

  • string equality testing is faster on string than const char *
  • Eliminate run-time overhead of implicit conversions from const char * to string that call strlen() on compile-time constant strings.
  • Compile-time string sets that do equality testing instead of hashing for size < N. (this is multiple cpus of overhead on one application I'm looking at)

I am trying to make a compile-time string class. I took a few hints from this post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str) } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; } constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

I am trying to make a compile-time string class. I took a few hints from this post. Unfortunately, I'm stuck on constructor overload precedence: the const char[] constructor is being ignored in favor of the const char* constructor. Any tips would be appreciated!

class string { public: // Can be done compile time. Works lovely! except... template<size_t N> constexpr string(const char(&char_array)[N]) : ptr_(char_array), length_(N-1) {} // This override gets called instead. I *must* keep this constructor. string(const char* const str) : ptr_(str) { length_ = strlen(str) } // Ugly hack. (not acceptable) template<size_t N> constexpr string(const char(&char_array)[N], double unused) : ptr_(char_array), length_(N-1) {} private: const char* ptr_; int length_; } constexpr const char kConstant[] = "FooBarBaz"; constexpr string kString(kConstant); // Error: constexpr variable 'kString' must be initialized by a constant expression (tries to call wrong overload) constexpr string kString(kConstant, 1.0f); // ugly hack works. 

There's lots of cool things I can do if I can make compile-time string constants.

  • string equality testing is faster on string than const char *
  • Eliminate run-time overhead of implicit conversions from const char * to string that call strlen() on compile-time constant strings.
  • Compile-time string sets that do equality testing instead of hashing for size < N. (this is multiple cpus of overhead on one application I'm looking at)
Source Link
Dave Dopson
  • 42.9k
  • 21
  • 97
  • 85
Loading