为什么 SFINAE 会在它应该起作用的地方导致编译器错误?

发布于 2024-11-18 08:37:02 字数 1384 浏览 4 评论 0原文

我试图实现一个元程序,它可以查找给定的指针类型是否为 const 。即

  • is_const::value 应该是 false
  • is_const::value 应该是 true

以下是代码:

template<class TYPE>
struct is_const
{
  typedef char yes[3];
  template<typename T>
  struct Perform
  {
    static yes& check (const T*&);
    static char check (T*&);
  };  

  TYPE it; 
  enum { value = (sizeof(Perform<TYPE>::check(it)) == sizeof(yes)) };  
};

编译器错误消息是:

In instantiation of ‘is_const<int*>’:
instantiated from here
error: no matching function for call to ‘is_const<int*>::Perform<int*>::check(int*&)’
note: candidates are: static char (& is_const<TYPE>::Perform<T>::check(const T*&))[3] [with T = int*, TYPE = int*]
note: static char is_const<TYPE>::Perform<T>::check(T*&) [with T = int*, TYPE = int*]

我的焦点已转移到错误消息。如果您看到最后一行:

note: static char is_const<TYPE>::Perform<T>::check(T*&) [with T = int*, TYPE = int*]

如果我们真的替换 T = int*TYPE = int* 那么它确实应该匹配适当的函数 (char check() )。我很想知道这里出了什么问题。

I was trying to implement a meta-program which finds if given pointer type is const or not. i.e.

  • is_const<TYPE*>::value should be false
  • is_const<const TYPE*>::value should be true

Following is the code:

template<class TYPE>
struct is_const
{
  typedef char yes[3];
  template<typename T>
  struct Perform
  {
    static yes& check (const T*&);
    static char check (T*&);
  };  

  TYPE it; 
  enum { value = (sizeof(Perform<TYPE>::check(it)) == sizeof(yes)) };  
};

And the compiler error messages are:

In instantiation of ‘is_const<int*>’:
instantiated from here
error: no matching function for call to ‘is_const<int*>::Perform<int*>::check(int*&)’
note: candidates are: static char (& is_const<TYPE>::Perform<T>::check(const T*&))[3] [with T = int*, TYPE = int*]
note: static char is_const<TYPE>::Perform<T>::check(T*&) [with T = int*, TYPE = int*]

My focus has shifted to the error message. If you see the last line:

note: static char is_const<TYPE>::Perform<T>::check(T*&) [with T = int*, TYPE = int*]

If we really replace T = int* and TYPE = int* then it really should match the appropriate function (char check()). I am anxious to know that what is going wrong here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千纸鹤 2024-11-25 08:37:02

为何如此迂回?一个简单的特征类怎么样:

#include <functional>

template <typename T> struct is_const_ptr : std::false_type { };
template <typename T> struct is_const_ptr<const T *> : std::true_type { };

struct Foo {};

int main()
{
  std::cout << is_const_ptr<Foo*>::value << is_const_ptr<const Foo*>::value << std::endl;
}

Why so roundabout? How about a straight-forward trait class:

#include <functional>

template <typename T> struct is_const_ptr : std::false_type { };
template <typename T> struct is_const_ptr<const T *> : std::true_type { };

struct Foo {};

int main()
{
  std::cout << is_const_ptr<Foo*>::value << is_const_ptr<const Foo*>::value << std::endl;
}
止于盛夏 2024-11-25 08:37:02

这是您的问题:

static yes& check (const T*&);
static char check (T*&);

当您实例化 is_const 时,您的函数定义将扩展为:

static yes& check (const int**&);
static char check (int**&);

但是,您的临时项 (TYPE it) 的类型为 int*,就像您指定的那样。您需要更改 check 函数签名以删除指针说明符,如下所示:

static yes& check (const T&);
static char check (T&);

Here's your problem:

static yes& check (const T*&);
static char check (T*&);

When you instantiate is_const<int*>, your function definitions are expanded to:

static yes& check (const int**&);
static char check (int**&);

However, your temporary item (TYPE it) is of type int*, like you specified. You need to change your check function signatures to remove the pointer specifier, like so:

static yes& check (const T&);
static char check (T&);
以往的大感动 2024-11-25 08:37:02

您的代码中有两处错误。

首先,以下内容

static yes& check (const T*&);
static char check (T*&);

必须更改为

static yes& check (const T&);
static char check (T&);

Ans 其次,it 成员必须是 static

static TYPE it;

,或者只需将 ((TYPE)0) 传递给您的检查功能。不需要会员。

THere are two things wrong in your code.

First, the following

static yes& check (const T*&);
static char check (T*&);

must be changed to

static yes& check (const T&);
static char check (T&);

Ans second, the it member must be static

static TYPE it;

or, just pass ((TYPE)0) to your check function. No need of the member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文