About operator ""_ に"

Asked 2 years ago, Updated 2 years ago, 121 views


located on page 309 of N3797 (pdf)

template<char...>double operator"_\u03C0();

I don't understand the meaning of this.
If the argument is empty, will 2 of 2_ の be ignored?
Or what is the difference between constexpr and _ なら?
Why is it a parameter pack?

c++ c++11

2022-09-29 21:53

2 Answers

It is not ignored, but a string before _ 以前 is passed as a parameter pack of char.

For example, 14142_ < is the same as calling operator"_ _<'1', '4', '4', '2'>().
The following sample restores integer values from the parameter pack.

#include<iostream>
# include <cmath>

template<typename T>
double decimal (T value) {
    return value - '0';
}

template<typename T, typename...Targs>
double decimal (T value, Targs...Fargs) {
    return(value-'0')*power(10,sizeof...(Fargs))+decimal(Fargs...);
}

template<char...T>double operator"_ ((){
    return decimal (T...);
}

int main() {
    std::cout<<14142_ ;;
}

http://melpon.org/wandbox/permlink/Z0SiejoGSos13ooN


2022-09-29 21:53

As @h2so5 replied, the 2 part is passed to the parameter pack of the template one character at a time (type char).At this time, the argument part must be empty.( 13 13.5.8/parograph 5)

5 The declaration of a literal operator template shall have an empty parameter-declaration-clause and its template-parameter-listshall have a single template-parameter that is a non-type template parameter(14/packet.

Or what is the difference between constexpr and _ なら?
Why is it a parameter pack?

The purpose of this piece of code is to show that UCN (universal-character-name) can be used for the identifier of User-defined literals, so it's no use pursuing more semantics.


2022-09-29 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.