Questions tagged «typename»

6
为什么必须在何处以及为什么要放置“模板”和“类型名”关键字?
在模板,在那里,为什么我必须把typename和template上依赖的名字呢? 无论如何,从属名称到底是什么? 我有以下代码: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // …

10
C ++获取模板中的类型名称
我正在编写一些用于解析某些文本数据文件的模板类,因此很可能大部分解析错误是由于数据文件中的错误而引起的,这些错误大部分不是程序员编写的,因此需要关于应用为何无法加载的好消息,例如: 解析example.txt时出错。[MySectiom]键的值(“ notaninteger”)不是有效的int 我可以从传递给模板函数的参数以及类中的成员vars得出文件名,节名和键名,但是我不确定如何获取模板函数尝试转换为的类型的名称。 我当前的代码看起来像,只对纯字符串等进行了专门化处理: template<typename T> T GetValue(const std::wstring &section, const std::wstring &key) { std::map<std::wstring, std::wstring>::iterator it = map[section].find(key); if(it == map[section].end()) throw ItemDoesNotExist(file, section, key) else { try{return boost::lexical_cast<T>(it->second);} //needs to get the name from T somehow catch(...)throw ParseError(file, section, key, it->second, TypeName(T)); } } 宁可不必对数据文件可能使用的每种类型都进行特定的重载,因为它们有很多负载... 我还需要一个解决方案,除非出现异常,否则不会产生任何运行时开销,即我想要一个完全编译时的解决方案,因为此代码被称为“成千上万次”,并且加载时间已经有些长了。 编辑:好的,这是我想出的解决方案: …

2
为什么在C ++ 20中的依赖类型之前不需要指定“ typename”?
这部分代码在C ++ 20中编译(使用gcc 10.1),而typename在依赖类型之前未使用关键字std::vector<T>::iterator。为什么要编译? #include <vector> template<typename T> std::vector<T>::iterator // Why does this not require "typename" before it? f() { return {}; } int main() { auto fptr = &f<int>; } 代码游乐场
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.