为什么在以下情况下不需要对依赖类型使用typename?

问题描述 投票:5回答:3

我一直在阅读有关删除type, here的引用的信息>

我在链接中看到了以下代码

type

#include <iostream> // std::cout #include <type_traits> // std::is_same template<class T1, class T2> void print_is_same() { std::cout << std::is_same<T1, T2>() << '\n'; } int main() { std::cout << std::boolalpha; print_is_same<int, int>(); print_is_same<int, int &>(); print_is_same<int, int &&>(); print_is_same<int, std::remove_reference<int>::type>(); // Why not typename std::remove_reference<int>::type ? print_is_same<int, std::remove_reference<int &>::type>();// Why not typename std::remove_reference<int &>::type ? print_is_same<int, std::remove_reference<int &&>::type>();// Why not typename std::remove_reference<int &&>::type ? } 特性中的type是从属类型。

可能的实现

std::remove_reference

但是为什么它没有被使用template< class T > struct remove_reference {typedef T type;}; template< class T > struct remove_reference<T&> {typedef T type;}; template< class T > struct remove_reference<T&&> {typedef T type;};

我一直在阅读有关删除该类型的引用的信息,在这里我在链接中包含以下代码#include // std :: cout #include // std :: is_same模板<...>

c++ c++11 templates dependencies typetraits
3个回答
10
投票

typename std::remove_reference</*TYPE*/>::type特性中的type是从属类型。


0
投票

简而言之,您需要std::remove_reference<T>::type以确保编译器具有以下功能:>

T

0
投票

您没看错,他们可以被视为对自己的依赖。但是,此处标准不需要main。只有合格的标识符才需要:

[temp.res]

© www.soinside.com 2019 - 2024. All rights reserved.