typename

Why don't I need to specify “typename” before a dependent type in C++20?

人走茶凉 提交于 2020-06-09 07:52:31
问题 This bit of code compiled in C++20 (using gcc 10.1) without using the typename keyword before the dependent type std::vector<T>::iterator . Why does it compile? #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>; } code playground 回答1: One of the new features in C++20 is Down with typename. In C++17, you had to provide the typename keyword in nearly all † dependent contexts to

Where and why do I have to put the “template” and “typename” keywords?

徘徊边缘 提交于 2020-03-03 06:57:46
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: 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> // For the last node Tn. struct UnionNode<T, void> { //

Use of typename keyword with typedef and new

本秂侑毒 提交于 2020-01-09 03:27:05
问题 Consider this code, template<class T> struct Sample { typename T::X *x; //declare pointer to T's X }; In the above code, the keyword typename is required by the compiler, so that it can disambiguate between nested types and nested values in templates. That means, in the absence of typename keyword, compiler would interpret this as multiplication of T::X with x, T::X *x; //multiply T::X with x So in such situations where ambiguity can arise, the keyword typename becomes necessity so as to

Implementing Scalar and Vector Addition for Custom Type with std::transform

谁都会走 提交于 2020-01-06 12:53:09
问题 This looks to me like a basic problem so apologies in advance for duplicate posts. Not sure what the terminology is though. I have a class, say my_data , for storing numbers. It has the basic constructors etc. and it would be handy to support elementary operations such as adding to the elements, e.g. increasing the values in the data. This is the most elementary way to describe the class: #include <stdlib.h> #include <vector> #include <iostream> #include <algorithm> #include <numeric>

How to get a Type from a C# type name string?

狂风中的少年 提交于 2019-12-31 06:54:05
问题 I've been looking at return values for Type.Namespace , Type.Name , Type.FullName , and Type.AssemblyQualifiedName . There are inconsistencies. For an inner class like ConsoleApplication8.Program+InnerClass , Namespace returns ConsoleApplication8 and Name returns InnerClass , omitting Program , so concatenating Type.NameSpace and Type.Name would be an incomplete representation of the class name (just as an example). Even the FullName property is inconsistent. Although it omits assembly name

How to get a Type from a C# type name string?

两盒软妹~` 提交于 2019-12-31 06:53:47
问题 I've been looking at return values for Type.Namespace , Type.Name , Type.FullName , and Type.AssemblyQualifiedName . There are inconsistencies. For an inner class like ConsoleApplication8.Program+InnerClass , Namespace returns ConsoleApplication8 and Name returns InnerClass , omitting Program , so concatenating Type.NameSpace and Type.Name would be an incomplete representation of the class name (just as an example). Even the FullName property is inconsistent. Although it omits assembly name

Why does not a template template parameter allow 'typename' after the parameter list

强颜欢笑 提交于 2019-12-30 09:45:08
问题 Template template typename? When using template template syntax as in template <template <typename> class T> , it is required to use the keyword class , as using typename gives an error along the lines of: error: template template parameter requires 'class' after the parameter list Everywhere else the keywords typename and class are interchangeable in the basic case of declaring a template parameter. You could argue that the requirement when using template template is a hint that you are

“typename” and “template” keywords: are they really necessary?

戏子无情 提交于 2019-12-29 06:28:11
问题 There are a lot of questions at this site with the problems while compiling c++ template code. One of the most common solutions to such problems is to add typename (and, less frequently, template ) keyword in the right places of the program code: template<typename T> class Base { public: typedef char SomeType; template<typename U> void SomeMethod(SomeType& v) { // ... } }; template<typename T> class Derived : public Base<T> { public: void Method() { typename Base<T>::SomeType x; // ^^^^^^^^

Use of typename keyword with template function parameters

烈酒焚心 提交于 2019-12-28 03:36:50
问题 In C++, the typename keyword is needed so the compiler can disambiguate between nested types and nested values in templates. However, there are certain situations where no ambiguity is possible, such as when a derived class inherits from a nested class type. template <class T> class Derived : public T::type { }; Here the typename keyword is not required, and is in fact not even allowed. This makes sense, because the context removes the ambiguity. Here, T::type must refer to a type, since you

C++ Compare template type during compile time

孤者浪人 提交于 2019-12-22 10:51:03
问题 I have a template class. Since the templates are processed during compile time, is it possible to compare the template parameter during compile time and use the preprocessor to add specific code? Something like this: template<class T> class MyClass { public: void do() { #if T is equal to vector<int> // add vector<int> specific code #if T is equal to list<double> // add list<double> specific code #else cout << "Unsupported data type" << endl; #endif } }; How can I compare the template types to