What is C++20's string literal operator template?
What is C++20's string literal operator template? Cppreference's example in this respect is quite concise and not very clear to me:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a();
In trying to understand what this feature is I've just learned that you can have numeric literal operator templates in C++, which make each digit of numerical constant be passed as a non-type argument to a template (cf. a better explanation here). Currently, literal operator templates do not work with character literals, though there are compilers extensions enabling that. I don't think C++20's string literal operator templates have anything to do with that as I've learned that proposals to extend literal operator templates to work with character literals were voted down in the commitee?
c++ c++20 user-defined-literals
add a comment |
What is C++20's string literal operator template? Cppreference's example in this respect is quite concise and not very clear to me:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a();
In trying to understand what this feature is I've just learned that you can have numeric literal operator templates in C++, which make each digit of numerical constant be passed as a non-type argument to a template (cf. a better explanation here). Currently, literal operator templates do not work with character literals, though there are compilers extensions enabling that. I don't think C++20's string literal operator templates have anything to do with that as I've learned that proposals to extend literal operator templates to work with character literals were voted down in the commitee?
c++ c++20 user-defined-literals
Does the proposal answer your question?
– Lightness Races in Orbit
1 hour ago
@LightnessRacesinOrbit That's the version that got voted down.
– cpplearner
50 mins ago
Okay, how about this one?
– Lightness Races in Orbit
49 mins ago
add a comment |
What is C++20's string literal operator template? Cppreference's example in this respect is quite concise and not very clear to me:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a();
In trying to understand what this feature is I've just learned that you can have numeric literal operator templates in C++, which make each digit of numerical constant be passed as a non-type argument to a template (cf. a better explanation here). Currently, literal operator templates do not work with character literals, though there are compilers extensions enabling that. I don't think C++20's string literal operator templates have anything to do with that as I've learned that proposals to extend literal operator templates to work with character literals were voted down in the commitee?
c++ c++20 user-defined-literals
What is C++20's string literal operator template? Cppreference's example in this respect is quite concise and not very clear to me:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a();
In trying to understand what this feature is I've just learned that you can have numeric literal operator templates in C++, which make each digit of numerical constant be passed as a non-type argument to a template (cf. a better explanation here). Currently, literal operator templates do not work with character literals, though there are compilers extensions enabling that. I don't think C++20's string literal operator templates have anything to do with that as I've learned that proposals to extend literal operator templates to work with character literals were voted down in the commitee?
c++ c++20 user-defined-literals
c++ c++20 user-defined-literals
asked 1 hour ago
lukeglukeg
1,6991825
1,6991825
Does the proposal answer your question?
– Lightness Races in Orbit
1 hour ago
@LightnessRacesinOrbit That's the version that got voted down.
– cpplearner
50 mins ago
Okay, how about this one?
– Lightness Races in Orbit
49 mins ago
add a comment |
Does the proposal answer your question?
– Lightness Races in Orbit
1 hour ago
@LightnessRacesinOrbit That's the version that got voted down.
– cpplearner
50 mins ago
Okay, how about this one?
– Lightness Races in Orbit
49 mins ago
Does the proposal answer your question?
– Lightness Races in Orbit
1 hour ago
Does the proposal answer your question?
– Lightness Races in Orbit
1 hour ago
@LightnessRacesinOrbit That's the version that got voted down.
– cpplearner
50 mins ago
@LightnessRacesinOrbit That's the version that got voted down.
– cpplearner
50 mins ago
Okay, how about this one?
– Lightness Races in Orbit
49 mins ago
Okay, how about this one?
– Lightness Races in Orbit
49 mins ago
add a comment |
1 Answer
1
active
oldest
votes
There were two separate proposals:
- Allowing string literals as non-type template parameters (P0424)
- Allowing class types as non-type template parameters (P0732)
The first proposal was partially merged into the second. String literals still are not valid arguments as non-type template parameters, but they are valid arguments into class types. The example from [temp.arg.nontype]/4 might help:
template<class T, T p> class X {
/* ... */
};
X<const char*, "Studebaker"> x; // error: string literal as template-argument
const char p = "Vivisectionist";
X<const char*, p> y; // OK
struct A {
constexpr A(const char*) {}
friend auto operator<=>(const A&, const A&) = default;
};
X<A, "Pyrophoricity"> z; // OK, string literal is a constructor argument to A
However, the part of the first proposal which extended the literal operators was what was merged into the second, [lex.ext]/5:
If S contains a literal operator template with a non-type template parameter for which str is a well-formed template-argument, the literal L is treated as a call of the form
operator "" X<str>()
So using this:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a() { return a; }
We can write "Hello"_a
, which will be interpreted as calling operator "" _a<A("Hello")>
.
Note that these rules are slightly in flux, as the defaulted <=>
requirement will be changing to a defaulted ==
requirement as per P1185.
Thanks for the explanation. As a sidenote, I must say that class-type being non-type parameter is one of many signs of this language's complexity.
– lukeg
24 mins ago
1
@lukeg I don't understand the comment. We've had non-type parameters for a long time. Inarray<int, 10>
,10
is a non-type template argument for the non-type template parameterN
. It's just that the kinds of types you could use for those parameters was very, very limited. Non-type just refers to the fact that the parameter is not a type - it's a value. A better name would value template parameter.
– Barry
11 mins ago
@lukeg: It's not an increase in the language's complexity; it's an increase in the language's complexity that you care about. Before, you probably never gave a thought to non-type template parameters or what types you could use with them. Now you do, so it seems more complex. But it isn't; the complexity was always there. There is simply a dimension of template parameters that used to be highly restricted, and now it isn't.
– Nicol Bolas
6 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54278201%2fwhat-is-c20s-string-literal-operator-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There were two separate proposals:
- Allowing string literals as non-type template parameters (P0424)
- Allowing class types as non-type template parameters (P0732)
The first proposal was partially merged into the second. String literals still are not valid arguments as non-type template parameters, but they are valid arguments into class types. The example from [temp.arg.nontype]/4 might help:
template<class T, T p> class X {
/* ... */
};
X<const char*, "Studebaker"> x; // error: string literal as template-argument
const char p = "Vivisectionist";
X<const char*, p> y; // OK
struct A {
constexpr A(const char*) {}
friend auto operator<=>(const A&, const A&) = default;
};
X<A, "Pyrophoricity"> z; // OK, string literal is a constructor argument to A
However, the part of the first proposal which extended the literal operators was what was merged into the second, [lex.ext]/5:
If S contains a literal operator template with a non-type template parameter for which str is a well-formed template-argument, the literal L is treated as a call of the form
operator "" X<str>()
So using this:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a() { return a; }
We can write "Hello"_a
, which will be interpreted as calling operator "" _a<A("Hello")>
.
Note that these rules are slightly in flux, as the defaulted <=>
requirement will be changing to a defaulted ==
requirement as per P1185.
Thanks for the explanation. As a sidenote, I must say that class-type being non-type parameter is one of many signs of this language's complexity.
– lukeg
24 mins ago
1
@lukeg I don't understand the comment. We've had non-type parameters for a long time. Inarray<int, 10>
,10
is a non-type template argument for the non-type template parameterN
. It's just that the kinds of types you could use for those parameters was very, very limited. Non-type just refers to the fact that the parameter is not a type - it's a value. A better name would value template parameter.
– Barry
11 mins ago
@lukeg: It's not an increase in the language's complexity; it's an increase in the language's complexity that you care about. Before, you probably never gave a thought to non-type template parameters or what types you could use with them. Now you do, so it seems more complex. But it isn't; the complexity was always there. There is simply a dimension of template parameters that used to be highly restricted, and now it isn't.
– Nicol Bolas
6 mins ago
add a comment |
There were two separate proposals:
- Allowing string literals as non-type template parameters (P0424)
- Allowing class types as non-type template parameters (P0732)
The first proposal was partially merged into the second. String literals still are not valid arguments as non-type template parameters, but they are valid arguments into class types. The example from [temp.arg.nontype]/4 might help:
template<class T, T p> class X {
/* ... */
};
X<const char*, "Studebaker"> x; // error: string literal as template-argument
const char p = "Vivisectionist";
X<const char*, p> y; // OK
struct A {
constexpr A(const char*) {}
friend auto operator<=>(const A&, const A&) = default;
};
X<A, "Pyrophoricity"> z; // OK, string literal is a constructor argument to A
However, the part of the first proposal which extended the literal operators was what was merged into the second, [lex.ext]/5:
If S contains a literal operator template with a non-type template parameter for which str is a well-formed template-argument, the literal L is treated as a call of the form
operator "" X<str>()
So using this:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a() { return a; }
We can write "Hello"_a
, which will be interpreted as calling operator "" _a<A("Hello")>
.
Note that these rules are slightly in flux, as the defaulted <=>
requirement will be changing to a defaulted ==
requirement as per P1185.
Thanks for the explanation. As a sidenote, I must say that class-type being non-type parameter is one of many signs of this language's complexity.
– lukeg
24 mins ago
1
@lukeg I don't understand the comment. We've had non-type parameters for a long time. Inarray<int, 10>
,10
is a non-type template argument for the non-type template parameterN
. It's just that the kinds of types you could use for those parameters was very, very limited. Non-type just refers to the fact that the parameter is not a type - it's a value. A better name would value template parameter.
– Barry
11 mins ago
@lukeg: It's not an increase in the language's complexity; it's an increase in the language's complexity that you care about. Before, you probably never gave a thought to non-type template parameters or what types you could use with them. Now you do, so it seems more complex. But it isn't; the complexity was always there. There is simply a dimension of template parameters that used to be highly restricted, and now it isn't.
– Nicol Bolas
6 mins ago
add a comment |
There were two separate proposals:
- Allowing string literals as non-type template parameters (P0424)
- Allowing class types as non-type template parameters (P0732)
The first proposal was partially merged into the second. String literals still are not valid arguments as non-type template parameters, but they are valid arguments into class types. The example from [temp.arg.nontype]/4 might help:
template<class T, T p> class X {
/* ... */
};
X<const char*, "Studebaker"> x; // error: string literal as template-argument
const char p = "Vivisectionist";
X<const char*, p> y; // OK
struct A {
constexpr A(const char*) {}
friend auto operator<=>(const A&, const A&) = default;
};
X<A, "Pyrophoricity"> z; // OK, string literal is a constructor argument to A
However, the part of the first proposal which extended the literal operators was what was merged into the second, [lex.ext]/5:
If S contains a literal operator template with a non-type template parameter for which str is a well-formed template-argument, the literal L is treated as a call of the form
operator "" X<str>()
So using this:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a() { return a; }
We can write "Hello"_a
, which will be interpreted as calling operator "" _a<A("Hello")>
.
Note that these rules are slightly in flux, as the defaulted <=>
requirement will be changing to a defaulted ==
requirement as per P1185.
There were two separate proposals:
- Allowing string literals as non-type template parameters (P0424)
- Allowing class types as non-type template parameters (P0732)
The first proposal was partially merged into the second. String literals still are not valid arguments as non-type template parameters, but they are valid arguments into class types. The example from [temp.arg.nontype]/4 might help:
template<class T, T p> class X {
/* ... */
};
X<const char*, "Studebaker"> x; // error: string literal as template-argument
const char p = "Vivisectionist";
X<const char*, p> y; // OK
struct A {
constexpr A(const char*) {}
friend auto operator<=>(const A&, const A&) = default;
};
X<A, "Pyrophoricity"> z; // OK, string literal is a constructor argument to A
However, the part of the first proposal which extended the literal operators was what was merged into the second, [lex.ext]/5:
If S contains a literal operator template with a non-type template parameter for which str is a well-formed template-argument, the literal L is treated as a call of the form
operator "" X<str>()
So using this:
struct A { A(const char *); auto operator<=>(const A&) const = default; };
template<A a> A operator ""_a() { return a; }
We can write "Hello"_a
, which will be interpreted as calling operator "" _a<A("Hello")>
.
Note that these rules are slightly in flux, as the defaulted <=>
requirement will be changing to a defaulted ==
requirement as per P1185.
answered 45 mins ago
BarryBarry
179k19310570
179k19310570
Thanks for the explanation. As a sidenote, I must say that class-type being non-type parameter is one of many signs of this language's complexity.
– lukeg
24 mins ago
1
@lukeg I don't understand the comment. We've had non-type parameters for a long time. Inarray<int, 10>
,10
is a non-type template argument for the non-type template parameterN
. It's just that the kinds of types you could use for those parameters was very, very limited. Non-type just refers to the fact that the parameter is not a type - it's a value. A better name would value template parameter.
– Barry
11 mins ago
@lukeg: It's not an increase in the language's complexity; it's an increase in the language's complexity that you care about. Before, you probably never gave a thought to non-type template parameters or what types you could use with them. Now you do, so it seems more complex. But it isn't; the complexity was always there. There is simply a dimension of template parameters that used to be highly restricted, and now it isn't.
– Nicol Bolas
6 mins ago
add a comment |
Thanks for the explanation. As a sidenote, I must say that class-type being non-type parameter is one of many signs of this language's complexity.
– lukeg
24 mins ago
1
@lukeg I don't understand the comment. We've had non-type parameters for a long time. Inarray<int, 10>
,10
is a non-type template argument for the non-type template parameterN
. It's just that the kinds of types you could use for those parameters was very, very limited. Non-type just refers to the fact that the parameter is not a type - it's a value. A better name would value template parameter.
– Barry
11 mins ago
@lukeg: It's not an increase in the language's complexity; it's an increase in the language's complexity that you care about. Before, you probably never gave a thought to non-type template parameters or what types you could use with them. Now you do, so it seems more complex. But it isn't; the complexity was always there. There is simply a dimension of template parameters that used to be highly restricted, and now it isn't.
– Nicol Bolas
6 mins ago
Thanks for the explanation. As a sidenote, I must say that class-type being non-type parameter is one of many signs of this language's complexity.
– lukeg
24 mins ago
Thanks for the explanation. As a sidenote, I must say that class-type being non-type parameter is one of many signs of this language's complexity.
– lukeg
24 mins ago
1
1
@lukeg I don't understand the comment. We've had non-type parameters for a long time. In
array<int, 10>
, 10
is a non-type template argument for the non-type template parameter N
. It's just that the kinds of types you could use for those parameters was very, very limited. Non-type just refers to the fact that the parameter is not a type - it's a value. A better name would value template parameter.– Barry
11 mins ago
@lukeg I don't understand the comment. We've had non-type parameters for a long time. In
array<int, 10>
, 10
is a non-type template argument for the non-type template parameter N
. It's just that the kinds of types you could use for those parameters was very, very limited. Non-type just refers to the fact that the parameter is not a type - it's a value. A better name would value template parameter.– Barry
11 mins ago
@lukeg: It's not an increase in the language's complexity; it's an increase in the language's complexity that you care about. Before, you probably never gave a thought to non-type template parameters or what types you could use with them. Now you do, so it seems more complex. But it isn't; the complexity was always there. There is simply a dimension of template parameters that used to be highly restricted, and now it isn't.
– Nicol Bolas
6 mins ago
@lukeg: It's not an increase in the language's complexity; it's an increase in the language's complexity that you care about. Before, you probably never gave a thought to non-type template parameters or what types you could use with them. Now you do, so it seems more complex. But it isn't; the complexity was always there. There is simply a dimension of template parameters that used to be highly restricted, and now it isn't.
– Nicol Bolas
6 mins ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54278201%2fwhat-is-c20s-string-literal-operator-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does the proposal answer your question?
– Lightness Races in Orbit
1 hour ago
@LightnessRacesinOrbit That's the version that got voted down.
– cpplearner
50 mins ago
Okay, how about this one?
– Lightness Races in Orbit
49 mins ago