Date: Tue, 20 May 2025 13:16:12 +0100
On Tue, May 20, 2025 at 9:02 AM Jonathan Wakely wrote:
>
>> template <typename ReturnType, typename... Params>
>> ReturnType ToReturnType( ReturnType (*)(Params...) )
>> {
>> return std::declval<ReturnType>();
>> }
>
>
> Which doesn't work if the type is not movable.
The following compiles fine for me. I had to take out the 'declval'
because the compiler refused to compile it.
#include <mutex> // mutex
#include <utility> // declval
template<typename T>
consteval decltype(auto) Func(void) noexcept
{
return *(T*)nullptr;
}
int main(void)
{
typedef decltype(Func<std::mutex>()) MyMutexType;
}
By the way, generally speaking, there are three ways to use declval:
declval<T >()
declval<T&&>()
delcval<T& >()
The first two yield an Xvalue. The third one yields an Lvalue. Would
it not have made more sense though for the first one to yield a
PRvalue? So then we could have had:
declval<T >() - PRvalue
declval<T&&>() - Xvalue
delcval<T& >() - Lvalue
Would that not have been better? Then we'd be able to use 'declval' to
return an unmovable type -- that's if the compiler doesn't refuse to
compile it.
>
>> template <typename ReturnType, typename... Params>
>> ReturnType ToReturnType( ReturnType (*)(Params...) )
>> {
>> return std::declval<ReturnType>();
>> }
>
>
> Which doesn't work if the type is not movable.
The following compiles fine for me. I had to take out the 'declval'
because the compiler refused to compile it.
#include <mutex> // mutex
#include <utility> // declval
template<typename T>
consteval decltype(auto) Func(void) noexcept
{
return *(T*)nullptr;
}
int main(void)
{
typedef decltype(Func<std::mutex>()) MyMutexType;
}
By the way, generally speaking, there are three ways to use declval:
declval<T >()
declval<T&&>()
delcval<T& >()
The first two yield an Xvalue. The third one yields an Lvalue. Would
it not have made more sense though for the first one to yield a
PRvalue? So then we could have had:
declval<T >() - PRvalue
declval<T&&>() - Xvalue
delcval<T& >() - Lvalue
Would that not have been better? Then we'd be able to use 'declval' to
return an unmovable type -- that's if the compiler doesn't refuse to
compile it.
Received on 2025-05-20 12:16:25