C::* 在 typename 参数中是什么意思

时间:2020-12-20 12:28:07

标签: c++ templates

我知道有类似的问题here

我的问题与那个略有不同, 模板参数中出现c::*是什么意思,

class TestActivity : AppCompatActivity(){

private val viewModel = TestViewModel(ServiceLocator.provideTasksRepository())
private lateinit var  binding : TestBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.test)
    binding.viewmodel = viewModel

    setupRecyclerView()
}

private fun setupRecyclerView() {
    binding.viewmodel?.run {
        binding.recyclerViewTest.adapter = TestAdapter(this)
    }
}

来自here

谁能解释一下这段代码是什么意思,尤其是这段

<块引用>

类型名称 C::const_iterator(C::*)()

2 个答案:

答案 0 :(得分:0)

<块引用>

出现 c::* 是什么意思

在上下文之外,它是指向 c 成员的指针的语法的一部分。

放在上下文中时:C::const_iterator(C::*)() const 类型是指向 C 的 const 限定成员函数的指针,该成员函数返回 C::const_iterator 并且具有空参数列表。

答案 1 :(得分:0)

恕我直言,那里的语句结构非常糟糕,无法阅读。但是可以分层“拆解”:

template <typename C>
static yes & f (
    typename std::enable_if<
        std::is_same<
            decltype(
                static_cast<
                     typename C::const_iterator(C::*)() const
                >(&C::begin)
            ),
            typename C::const_iterator(C::*)() const
        >::value
    >::type *
);

它声明了一个模板,该模板带有函数 f 的类参数 C,返回 yes&

如果 typename void*C::const_iterator(C::*)() const&C::begin 的转换结果类型相同,函数实例将有一个 C::const_iterator(C::*)() const 类型的参数,否则模板获胜'由于 SFINAE 不会产生法律实例,因为 std::enable_if<>::type 不会被声明(如果 std::enable_if<expr>::type 为真,void 默认为 expr

typename C::const_iterator(C::*)() const 是指向类 C 成员的指针,其签名类似于 C::const_iterator name () const,即它返回 C::const_iterator,不带参数,并且是一个 const 成员。在 C++20 之前的标准中,如果声明取决于模板参数,则必须使用 typename,似乎在最新标准中建议取消该要求。

static_cast<typename C::const_iterator(C::*)() const> 是否有可能在 C::begin 是重载函数成员时防止大小写,它会选择适当的重载。

相关问题