Declare variables that depend on unknown type in template functions.
        Posted  
        
            by rem
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by rem
        
        
        
        Published on 2010-05-22T12:52:49Z
        Indexed on 
            2010/05/22
            13:01 UTC
        
        
        Read the original article
        Hit count: 196
        
Suppose I'm writing a template function foo that has type parameter T. It gets an object of type T that must have method bar(). And inside foo I want to create a vector of objects of type returned by bar.
In GNU C++ I can write something like that:
template<typename T>
void foo(T x) {
    std::vector<__typeof(x.bar())> v;
    v.push_back(x.bar());
    v.push_back(x.bar());
    v.push_back(x.bar());
    std::cout << v.size() << std::endl;
}
How to do the same thing in Microsoft Visual C++? Is there some way to write this code that works in both GNU C++ and Visual C++?
© Stack Overflow or respective owner