是否可以检查是否为给定的类型和参数定义了用户文字?

xin*_*aiz 12 c++ templates sfinae c++17

我想在编译时检查是否_name为类型Ret和参数定义了用户文字Arg.虽然我有半解决方案,但它需要operator至少定义一次文字:

#include <iostream>
#include <type_traits>

struct one { };
struct two { };

// we need at least one of these definitions for template below to compile
one operator"" _x(char const*) {return {};}
two operator"" _x(unsigned long long int) {return {};}

template<class T, class S, class = void>
struct has_literal_x : std::false_type
{  };

template<class T, class S>
struct has_literal_x <T, S,
    std::void_t<decltype((T(*)(S))(operator"" _x))>
    > : std::true_type
{ };

int main()
{
    std::cout << has_literal_x<one, char const*>::value << std::endl;
    std::cout << has_literal_x<two, unsigned long long int>::value << std::endl;

    std::cout << has_literal_x<one, unsigned long long int>::value << std::endl;
    std::cout << has_literal_x<two, char const*>::value << std::endl;

    std::cout << has_literal_x<int, char const*>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

1
1
0
0
0
Run Code Online (Sandbox Code Playgroud)

但是,如果没有至少一个可能重载的用户文字的定义,则此解决方案将不起作用.有没有办法检查它甚至是不存在的文字(可能我们可以检查类X是否有成员member,但我不知道它是否在这种情况下可行)?

sky*_*ack 9

是否可以检查是否为给定的类型和参数定义了用户文字?

(简称)答案是肯定的.


例如,您可以在示例代码中使用以下特化:

template<class T, class S> 
struct has_literal_x <T, S,
      std::enable_if_t<std::is_same<decltype(operator""_x(std::declval<S>())), T>::value>
    > : std::true_type
{ };
Run Code Online (Sandbox Code Playgroud)

很快就会变成:

#include <iostream>
#include <type_traits>
#include <utility>

struct one { };
struct two { };

//one operator"" _x(char const*) { return {}; }
//two operator"" _x(unsigned long long int) { return {}; }

template<class T, class S, class = void>
struct has_literal_x : std::false_type
{  };

template<class T, class S> 
struct has_literal_x <T, S, 
      std::enable_if_t<std::is_same<decltype(operator""_x(std::declval<S>())), T>::value> 
    > : std::true_type
{ };

int main()
{  
    std::cout << has_literal_x<one, char const*>::value << std::endl;
    std::cout << has_literal_x<two, unsigned long long int>::value << std::endl;

    std::cout << has_literal_x<one, unsigned long long int>::value << std::endl;
    std::cout << has_literal_x<two, char const*>::value << std::endl;

    std::cout << has_literal_x<int, char const*>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

输出是预期的:0对所有输出.


另一种方法是,在C++ 14(主要是启发这个答案@ Jarod42的)是一个模板变量的手段.
举个例子:

template<typename T, typename S, typename = void>
constexpr bool has_literal_v = false;

template<typename T, typename S>
constexpr bool has_literal_v<T, S, std::enable_if_t<std::is_same<decltype(operator""_x(std::declval<S>())), T>::value>> = true;
Run Code Online (Sandbox Code Playgroud)

main将成为代替:

int main()
{  
    std::cout << has_literal_v<one, char const*> << std::endl;
    std::cout << has_literal_v<two, unsigned long long int> << std::endl;

    std::cout << has_literal_v<one, unsigned long long int> << std::endl;
    std::cout << has_literal_v<two, char const*> << std::endl;

    std::cout << has_literal_v<int, char const*> << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我觉得它很容易阅读,这是一个constexpr变量.还有什么?


Jar*_*d42 5

有了is_detected函数系列,你可能就是这么做的

template <typename T>
using has_literal_x_type = decltype(operator"" _x(std::declval<T>()));

template <typename Ret, typename T>
using has_literal_x = std::is_same<Ret, detected_t<has_literal_x_type, T>>;
Run Code Online (Sandbox Code Playgroud)

并测试它

static_assert(!has_literal_x<one, char const*>::value, "unexpected");
static_assert(!has_literal_x<one, unsigned long long int>::value, "unexpected");
static_assert(!has_literal_x<two, char const*>::value, "unexpected");
static_assert(!has_literal_x<two, unsigned long long int>::value, "unexpected");
static_assert(!has_literal_x<int, char const*>::value, "unexpected");
Run Code Online (Sandbox Code Playgroud)

演示