Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Note that the form shown here wasn't accepted into the standard, as noted below.

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).
This part of the proposal seems to have not been accepted.

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approachJerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 

Note that the form shown here wasn't accepted into the standard, as noted below.

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).
This part of the proposal seems to have not been accepted.

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 

Note that the form shown here wasn't accepted into the standard, as noted below.

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).
This part of the proposal seems to have not been accepted.

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 
added 88 characters in body
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238

Note that the form shown here wasn't accepted into the standard, as noted below.

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).
This part of the proposal seems to have not been accepted.

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).
This part of the proposal seems to have not been accepted.

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 

Note that the form shown here wasn't accepted into the standard, as noted below.

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).
This part of the proposal seems to have not been accepted.

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 
added 63 characters in body; Post Made Community Wiki
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).
This part of the proposal seems to have not been accepted.

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 

Compile time string processing is guessed to become possible through user-defined literals proposed in N2765.
As i already mentioned, i don't know of any compiler that currently implements it and without compiler support there can be only guess work.

In §2.13.7.3 and 4 of the draft we have the following:

Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X<'c1', 'c2', ... , 'ck'>() where n is the source character sequence c1c2...ck. [Note: The sequence c1c2...ck can only contain characters from the basic source character set. —end note]

Combine that with constexpr and we should have compile time string processing.

update: i overlooked that i was reading the wrong paragraph, this form is allowed for user-defined-integer-literals and -floating-literals, but apparently not for -string-literals (§2.13.7.5).
This part of the proposal seems to have not been accepted.

That being said, with my limited glimpse at C++0x, it might look something like this (i most likely got something wrong):

template<char c, char... str> struct hash { static const unsigned result = c + hash<str...>::result; }; template<char c> struct hash { static const unsigned result = c; }; template<char... str> constexpr unsigned operator "" _hash() { return hash<str>::result; } // update: probably wrong, because the above // form is not allowed for string-literals: const unsigned h = "abcd"_hash; 

If Jerrys approach works, then the following should work however:

constexpr unsigned operator "" _hash(const char* s, size_t) { return const_hash(s); } 
deleted 84 characters in body
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238
Loading
added 292 characters in body
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238
Loading
added 76 characters in body
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238
Loading
added 79 characters in body
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238
Loading
added 286 characters in body
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238
Loading
deleted 16 characters in body
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238
Loading
added 6 characters in body; added 3 characters in body
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238
Loading
Source Link
Georg Fritzsche
  • 99.4k
  • 26
  • 198
  • 238
Loading