typename

Cryptic template template parameter error

余生颓废 提交于 2019-12-22 08:52:42
问题 I'm trying to create a function that gets the keys from a std::map or an std::unordered_map . I could use a simple overload, but first I'd love to know what's wrong with this code. template<typename K, typename V, template<typename, typename> class TContainer> std::vector<K> getKeys(const TContainer<K, V>& mMap) { std::vector<K> result; for(const auto& itr(std::begin(mMap)); itr != std::end(mMap); ++itr) result.push_back(itr->first); return result; } When calling it with an std::unordered_map

multiple nested dependent names - where to stick the typename keyword?

拜拜、爱过 提交于 2019-12-21 03:55:37
问题 This question was inspired by this other question. While trying to answer that question, I understood that I have a lot of questions myself. So... Consider the following: struct S1 { enum { value = 42 }; }; template <class T> struct S2 { typedef S1 Type; }; template <class T> struct S3 { typedef S2<T> Type; }; template <class T> struct S4 { typedef typename T::Type::Type Type; //(1)//legal? enum {value = T::Type::Type::value }; //(2)//legal? }; int main() { S4<S3<S2<S2<S1> > > >::value; }

How can I determine if an object can ToString into value or type name?

…衆ロ難τιáo~ 提交于 2019-12-19 21:29:21
问题 I am writing an interop between a php service and our crm. One of the things I need to do is make sure that simple types get converted ToString() for use later in a json converter. I am not sure even what the name is for 'simple types' but it can be defined like this... "an object that represents a low level variable type, containing a single value, not a class or anything with executable functions etc" I've found that int, string, bool, double, and surprisingly enum will ToString() with

Is it possible to get the type name of a generic type?

与世无争的帅哥 提交于 2019-12-18 21:21:07
问题 I have a method signature of execute<TResult>(): Observable<TResult> How do I get the name of the TResult type? Example: execute<ViewModel> --> "ViewModel" is the result I need. 回答1: As far as I know it is not possible to get the name of TResult , but if you provide the accordingly constructor function you can get the name. Declaration: execute<TResult>(ctor: { new (): TResult }) : <TResult> { console.log(ctor.name) //Prints out SomeClass return <any>null; } Usage: execute<SomeClass>

When should I use the keyword “typename” when using templates

帅比萌擦擦* 提交于 2019-12-17 23:46:06
问题 I've been working lately on a small project, and I couldn't figure out something.. I've been given a .h file that was containing a class, using a typename template. Inside that class there was a private class. template <typename T> class Something { public: Something(); ~Something(); Node* Function1(int index); int Index(const T& id); private: class Node() { public: T id; //Imagine the rest for the Node }; }; The problem occured when I wanted to define the functions of the class "Something"

Unexpected results from typename

僤鯓⒐⒋嵵緔 提交于 2019-12-17 17:12:16
问题 I am getting some unexpected results from typename and am stumped. Hopefully some can point me in the right direction. Private Sub T() Dim d As Word.Document Dim s As String Dim c As Collection Dim i As Long Dim o As Object Set d = ActiveDocument s = "X" Set c = New Collection Debug.Print "d is a " & TypeName(d) Debug.Print "s is a " & TypeName(s) Debug.Print "c is a " & TypeName(c) c.Add (d) c.Add (s) For i = 1 To c.count Debug.Print "Item " & i & " of the collection is a " & " " & TypeName

Why is the keyword “typename” needed before qualified dependent names, and not before qualified independent names?

风格不统一 提交于 2019-12-17 08:53:17
问题 class A { static int iterator; class iterator { [...] }; [...] }; I (think I) understand the reason why typename is needed here: template <class T> void foo() { typename T::iterator* iter; [...] } but I don't understand the reason why typename is not needed here: void foo() { A::iterator* iter; [...] } Can anyone explain? EDIT: The reason why the compiler does not have a problem with the latter, I found to be answered well in a comment: in the case of A::iterator I don't see why the compiler

How to create a generic template based factory?

萝らか妹 提交于 2019-12-14 03:07:24
问题 I am using c++98. I want to create a generic factory which is template based, and the creator can take no parameter to create the target object, or one parameter. /*<class.h> begins #ifndef INCLUDED_CLASS #define INCLUDED_CLASS #include <iostream> #include <boost/shared_ptr.hpp> class A { public: A() { _a = 27; } void print() const { std::cout << "A is " << _a << "." << std::endl; } protected: int _a; }; typedef boost::shared_ptr<A> APtr; class AA : public A { public: void print() const { std

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

空扰寡人 提交于 2019-12-13 03:24:54
问题 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> { //

Is typename required or not here?

懵懂的女人 提交于 2019-12-12 11:35:32
问题 Consider the code: #include <memory> template <class T, class Deleter = std::default_delete<T>> class unique_ptr_wrapper: public std::unique_ptr<T, Deleter> { public: using typename std::unique_ptr<T, Deleter>::unique_ptr; operator T* () const {return this->get();} }; int main() { unique_ptr_wrapper<int> upw{new int{42}}; } g++5.1 compiles it fine, although clang++ complains error: typename is allowed for identifiers only I agree that we don't have an identifier here, so probably typename is