I want to write a class template, which:
A<float>have just a member variableval;A<int>have a member variablevaland a member functionmake_unsigned(returnunsigned).
I want to explicitly state the return value of make_unsigned. Here is my code.
#include <concepts>
template<typename T> struct A
{
T val;
std::make_unsigned_t<T> make_unsigned() requires std::signed_integral<T>
{
return static_cast<std::make_unsigned<T>::type>(val);
}
};
int main()
{
A<float> val;
return 0;
}
Also I have tried auto make_unsigned() requires std::signed_integral<T> -> std::make_unsigned_t<T> and auto make_unsigned() -> std::make_unsigned_t<T> requires std::signed_integral<T>, but all of them cannot be compiled. It complains:
~ LANG=C g++ main.cpp -std=c++20 -o main
In file included from /usr/include/c++/10.2.0/bits/move.h:57,
from /usr/include/c++/10.2.0/bits/stl_pair.h:59,
from /usr/include/c++/10.2.0/utility:70,
from /usr/include/c++/10.2.0/array:38,
from main.cpp:1:
/usr/include/c++/10.2.0/type_traits: In instantiation of ‘struct std::make_unsigned<float>’:
/usr/include/c++/10.2.0/type_traits:1965:11: required by substitution of ‘template<class _Tp> using make_unsigned_t = typename std::make_unsigned::type [with _Tp = float]’
main.cpp:8:29: required from ‘struct A<float>’
main.cpp:17:14: required from here
/usr/include/c++/10.2.0/type_traits:1826:62: error: invalid use of incomplete type ‘class std::__make_unsigned_selector<float, false, false>’
1826 | { typedef typename __make_unsigned_selector<_Tp>::__type type; };
| ^~~~
/usr/include/c++/10.2.0/type_traits:1733:11: note: declaration of ‘class std::__make_unsigned_selector<float, false, false>’
1733 | class __make_unsigned_selector;
Or:
~ LANG=C g++ main.cpp -std=c++20 -o main
main.cpp:8:59: error: base operand of ‘->’ is not a pointer
8 | auto make_unsigned() requires std::signed_integral<T> -> std::make_unsigned_t<T>
| ^~
main.cpp:8:62: error: invalid use of ‘using make_unsigned_t = typename std::make_unsigned<_Tp>::type’
8 | auto make_unsigned() requires std::signed_integral<T> -> std::make_unsigned_t<T>
| ^~~
cc1plus: error: expression must be enclosed in parenthese
Or:
~ LANG=C g++ main.cpp -std=c++20 -o main
In file included from /usr/include/c++/10.2.0/bits/move.h:57,
from /usr/include/c++/10.2.0/bits/stl_pair.h:59,
from /usr/include/c++/10.2.0/utility:70,
from /usr/include/c++/10.2.0/array:38,
from main.cpp:1:
/usr/include/c++/10.2.0/type_traits: In instantiation of ‘struct std::make_unsigned<float>’:
/usr/include/c++/10.2.0/type_traits:1965:11: required by substitution of ‘template<class _Tp> using make_unsigned_t = typename std::make_unsigned::type [with _Tp = float]’
main.cpp:8:10: required from ‘struct A<float>’
main.cpp:17:14: required from here
/usr/include/c++/10.2.0/type_traits:1826:62: error: invalid use of incomplete type ‘class std::__make_unsigned_selector<float, false, false>’
1826 | { typedef typename __make_unsigned_selector<_Tp>::__type type; };
| ^~~~
/usr/include/c++/10.2.0/type_traits:1733:11: note: declaration of ‘class std::__make_unsigned_selector<float, false, false>’
1733 | class __make_unsigned_selector;
How can I do it?