Why isn't this working (c++ templates)?

Started by
7 comments, last by Beosar 9 years ago
Hi,

the following code doen't work and I don't know why:
template <typename T, size_t ReserveSize=16, size_t MemoryPoolNumElements = 8192, size_t Alignment=16, size_t NumPagesPerMemoryPool = 16, typename T_Size=uint16_t, uint8_t MemoryPoolGroupElementsExp=5>
using Vector = MemoryPool<T, MemoryPoolNumElements, Alignment, NumPagesPerMemoryPool, T_Size>::MultipleObjectsAllocator<MemoryPoolGroupElementsExp>::Vector < ReserveSize > ;
I get some compiler errors, including C2039: Vector is not an element of "global namespace".

In class MemoryPool:
template <uint8_t GroupElementsExp=3>
    /*
    A memory pool which allows allocation of multiple objects per pointer.
    */
    using MultipleObjectsAllocator = MultiElementMemoryPool<T, ElementsPerPage, Alignment, T_Size, GroupElementsExp>;
In class MultiElementMemoryPool:
template <size_t ReserveSize = 16>
        /*
        A vector.
        */
        using Vector = Vector_t<T,ReserveSize,ElementsPerPage,Alignment,NumPagesPerMemoryPool,T_Size,GroupElementsExp>;
Vector_t is declared inside MultiElementMemoryPool and MultiElementMemoryPool is declared inside MemoryPool.

The following does work, but it's not what I want:
template <typename T, size_t ReserveSize=16, size_t MemoryPoolNumElements = 8192, size_t Alignment=16, size_t NumPagesPerMemoryPool = 16, typename T_Size=uint16_t, uint8_t MemoryPoolGroupElementsExp=5>
using Vector = MemoryPool<T, MemoryPoolNumElements, Alignment, NumPagesPerMemoryPool, T_Size>;
Advertisement

do std::vector

Vector is in the std namespace so at the top you can do "use namespace std;" or you have to prepend std:: to vector.

I don't want to use std::vector, I already have a Vector in class MemoryPool, but I do not want to write the long name all the time and write e.g. Vector<char> instead.
Are you using a compiler which actually supports 'using' as a more capable typedef replacement? I'm not sure about the newer versions, but I know MSVC 2012 can't do it for example.

Edit: You also should get into the habit of pasting the complete errors. You only mentioned one error and not even with the complete error text.

I use Visual Studio 2013, so the compiler is Microsoft Visual C++ 2013 I think.

The other compiler errors are syntax errors in another file which disappear when I remove the using Vector = ... statement.

The using statement is supported I think, but it doesn't work with templated classes inside templated classes when the using statement itself is templated. template <typename T> using C = A<int>::B<T>; does work, but when I write A<T>::B<T> instead, it doesn't work.

Edit: https://msdn.microsoft.com/en-us/library/71dw8xzh.aspx says I should use template <typename T> template <typename T2>. This seems to be the solution, I will try it later.

Edit2: This doesn't work :(

Try to give as a minimal example, and give us the error messages.

I Can't go in details but I think it has something to do with this:

A simpler example:


class A
{
    class B {};
};

template<class T>
void foo(){
// for some reason we want to instantiate T::B, however 
// just T::B var; won't do the job (at least for gcc, msvc will do it i guess)
typename T::B var; // this will do the job
}
From the forum FAQ and sidebar:

In case the article linked under Point 1 above didn't make it clear, the error message is very important. Include a complete copy of the error in your post: all the numbers, codes, scary-looking gibberish, everything. This information will go a long way towards helping an experienced programmer spot the problem.


Please include the actual exact error message, and the actual exact code that is mentioned in the error message.

template <typename T, size_t MemoryPoolNumElements = 8192, size_t Alignment=16, size_t NumPagesPerMemoryPool = 16, typename T_Size=uint16_t, uint8_t MemoryPoolGroupElementsExp=5, size_t ReserveSize=16>
using Vector = typename MemoryPool<T, MemoryPoolNumElements, Alignment, NumPagesPerMemoryPool, T_Size>::template MultipleObjectsAllocator<MemoryPoolGroupElementsExp>::template Vector < ReserveSize > ;

Does this work? The "typename" in front of MemoryPool is important because you are using an inner type(def) of a templateed:

edited: A and those extra ::template identifiers are important. I found this link which should explain everything.

Thank you, simber, that worked.

This topic is closed to new replies.

Advertisement