可以SFINAE检测私人访问违例吗? [英] Can SFINAE detect private access violations?

查看:145
本文介绍了可以SFINAE检测私人访问违例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果我测试一个类的成员和成员是私人什么将sfinae响应?

解决方案

是的。



EDIT: C ++ 11 §14.8.2[temp.deduct] 标准报价


8 / 如果替换导致无效的类型或表达式,则类型扣除失败。无效的类型或表达式是如果使用替换参数写入将是不成形的。 [注意:访问检查是替换过程的一部分。 -end note]


这表明 private 可能触发SFINAE错误。阅读:


只有在函数类型及其模板参数类型的上下文中无效的类型和表达式才能导致扣除失败。注意:替换类型和表达式的求值
可能导致副作用,例如类模板特化和/或函数模板特化的实例化,隐式定义函数的生成等。这种边结束注释]




EDIT结束 / p>

所以在我看来,它会以SFINAE的方式错误,这进一步证实了从Clang摘录:

  // clang / Basic / DiagnosticIDs.h:185-209 

/// \brief枚举描述诊断程序的发射
///在C ++模板参数扣除期间发生时被处理。
枚举SFINAEResponse {
/// \brief诊断不应该报告,但它应该导致
///模板参数扣除失败。
///
///在模板参数
///扣除期间发生的绝大多数错误属于此类别。
SFINAE_SubstitutionFailure,

/// \brief应该完全抑制诊断。
///
///警告通常属于此类别。
SFINAE_Suppress,

/// \brief应报告诊断。
///
///应报告诊断。各种致命错误(例如,
///模板实例化深度超出)属于此类别。
SFINAE_Report,

/// \brief诊断是一个访问控制诊断,将
///在某些上下文中替换失败并在其他上下文中报告。
SFINAE_AccessControl
};

在SFINAE的情况下,访问控制有一些特殊情况。


I wonder whether if i test for some member of a class and the member is private what will sfinae respond? Will it error out hard or will it say ok or will it error out in the sfinae way?

解决方案

Yes.

EDIT: C++11 Standard quote from §14.8.2 [temp.deduct]

8/ If a substitution results in an invalid type or expression, type deduction fails. An invalid type or expression is one that would be ill-formed if written using the substituted arguments. [ Note: Access checking is done as part of the substitution process. —end note ]

This suggests to me that private can trigger an SFINAE error. Reading on:

Only invalid types and expressions in the immediate context of the function type and its template parameter types can result in a deduction failure. [ Note: The evaluation of the substituted types and expressions can result in side effects such as the instantiation of class template specializations and/or function template specializations, the generation of implicitly-defined functions, etc. Such side effects are not in the "immediate context" and can result in the program being ill-formed.—end note ]

The "immediate context" is not so clear to me... but it does not contradict my point :)

end of EDIT

So it seems to me that it will error out in an SFINAE way, this is further confirmed by this excerpt from Clang:

// clang/Basic/DiagnosticIDs.h:185-209

  /// \brief Enumeration describing how the the emission of a diagnostic should
  /// be treated when it occurs during C++ template argument deduction.
  enum SFINAEResponse {
    /// \brief The diagnostic should not be reported, but it should cause
    /// template argument deduction to fail.
    ///
    /// The vast majority of errors that occur during template argument
    /// deduction fall into this category.
    SFINAE_SubstitutionFailure,

    /// \brief The diagnostic should be suppressed entirely.
    ///
    /// Warnings generally fall into this category.
    SFINAE_Suppress,

    /// \brief The diagnostic should be reported.
    ///
    /// The diagnostic should be reported. Various fatal errors (e.g.,
    /// template instantiation depth exceeded) fall into this category.
    SFINAE_Report,

    /// \brief The diagnostic is an access-control diagnostic, which will be
    /// substitution failures in some contexts and reported in others.
    SFINAE_AccessControl
  };

There are special cases with regard to Access Control in the case of SFINAE.

这篇关于可以SFINAE检测私人访问违例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆