vendredi 14 décembre 2018

using typedef-ed type as class template argument

I have the following typedef for uint32:

typedef unsigned long long uint32;

Also I have the generic templated class for Hashmap with the following declaration:

template<typename K, typename V>
class Hashmap {

...


public:
    explicit Hashmap(uint32 size, float loadFactor = 0.75f) {}

...

}

I use the Hashmap class in another class with actual template parameters uint32 and uint32.

class Interpreter {
private:
    uint8 memory[MEMORY_SIZE] = {0};
    uint32 pointer = 0;
    uint32 programCounter = 0;
    std::string program;
    std::string outputBuffer;
    Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256);

...

This however for some reason does not compile and gives a lot of unrelated (in my opinion) and different errors across all the files. (The complete list of them is at the end of the question)

But if I replace the uint32 in the Hashmap constructor invocation with unsigned long long the code magically compiles and all the errors disappear. What is even more strange is that the same code compiles on some compilers but not on this one.

Hashmap<uint32, uint32>* jumpTable = new Hashmap<unsigned long long, unsigned long long>(256);

What is the reason for this behaviour? Why is this happening? And how can I write the class to be compilable on different compilers?


I am using CMake which is using c++:

$ c++ -v
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)

Complete list of errors:

In file included from /src/Interpreter.cpp:1:0:
/src/Interpreter.h:30:62: error: expected ‘;’ at end of member declaration
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                              ^
/src/Interpreter.h:30:62: error: declaration of ‘Hashmap<long long unsigned int, long long unsigned int> Interpreter::uint32’ [-fpermissive]
In file included from /src/Interpreter.h:6:0,
                 from /src/Interpreter.cpp:1:
/src/types.h:9:28: error: changes meaning of ‘uint32’ from ‘typedef long long unsigned int uint32’ [-fpermissive]
 typedef unsigned long long uint32;
                            ^
In file included from /src/Interpreter.cpp:1:0:
/src/Interpreter.h:30:68: error: expected unqualified-id before ‘>’ token
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                                    ^
/src/Interpreter.h:30:54: error: wrong number of template arguments (1, should be 2)
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                      ^
In file included from /src/Interpreter.h:7:0,
                 from /src/Interpreter.cpp:1:
/src/Hashmap.h:17:7: error: provided for ‘template<class K, class V> class Hashmap’
 class Hashmap {
       ^
/src/Interpreter.cpp: In member function ‘void Interpreter::opJmpFwd()’:
/src/Interpreter.cpp:67:16: error: expected ‘;’ before ‘paren’
         uint32 paren = 1;
                ^
/src/Interpreter.cpp:68:16: error: expected ‘;’ before ‘pos’
         uint32 pos = programCounter + 1;
                ^
/src/Interpreter.cpp:71:25: error: ‘pos’ was not declared in this scope
             if (program[pos] == OP_JMP_BK) {
                         ^
/src/Interpreter.cpp:72:17: error: ‘paren’ was not declared in this scope
                 paren--;
                 ^
/src/Interpreter.cpp:79:25: error: ‘pos’ was not declared in this scope
             if (program[pos] == OP_JMP_FW) {
                         ^
/src/Interpreter.cpp:80:17: error: ‘paren’ was not declared in this scope
                 paren++;
                 ^
/src/Interpreter.cpp:83:13: error: ‘pos’ was not declared in this scope
             pos++;
             ^
/src/Interpreter.cpp:92:40: error: ‘pos’ was not declared in this scope
         jumpTable->put(programCounter, pos + 1);
                                        ^
/src/Interpreter.cpp: In member function ‘void Interpreter::opJmpBk()’:
/src/Interpreter.cpp:112:16: error: expected ‘;’ before ‘paren’
         uint32 paren = 1;
                ^
/src/Interpreter.cpp:113:16: error: expected ‘;’ before ‘pos’
         uint32 pos = programCounter - 1;
                ^
/src/Interpreter.cpp:117:25: error: ‘pos’ was not declared in this scope
             if (program[pos] == OP_JMP_FW) {
                         ^
/src/Interpreter.cpp:118:17: error: ‘paren’ was not declared in this scope
                 paren--;
                 ^
/src/Interpreter.cpp:125:25: error: ‘pos’ was not declared in this scope
             if (program[pos] == OP_JMP_BK) {
                         ^
/src/Interpreter.cpp:126:17: error: ‘paren’ was not declared in this scope
                 paren++;
                 ^
/src/Interpreter.cpp:129:13: error: ‘pos’ was not declared in this scope
             pos--;
             ^
/src/Interpreter.cpp:138:40: error: ‘pos’ was not declared in this scope
         jumpTable->put(programCounter, pos);
                                        ^
/src/Interpreter.cpp: In member function ‘void Interpreter::loadProgram(const string&)’:
/src/Interpreter.cpp:170:43: error: type/value mismatch at argument 1 in template parameter list for ‘template<class K, class V> class Hashmap’
     jumpTable = new Hashmap<uint32, uint32>(256);
                                           ^
/src/Interpreter.cpp:170:43: error:   expected a type, got ‘((Interpreter*)this)->Interpreter::uint32’
/src/Interpreter.cpp:170:43: error: type/value mismatch at argument 2 in template parameter list for ‘template<class K, class V> class Hashmap’
/src/Interpreter.cpp:170:43: error:   expected a type, got ‘((Interpreter*)this)->Interpreter::uint32’
/src/Interpreter.cpp: In member function ‘std::string Interpreter::interpret()’:
/src/Interpreter.cpp:180:12: error: expected ‘;’ before ‘programLength’
     uint32 programLength = program.length();
            ^
/src/Interpreter.cpp:183:30: error: ‘programLength’ was not declared in this scope
     while (programCounter != programLength) {
                              ^

In file included from /src/main.cpp:3:0:
/src/Interpreter.h:30:62: error: expected ‘;’ at end of member declaration
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                              ^
/src/Interpreter.h:30:62: error: declaration of ‘Hashmap<long long unsigned int, long long unsigned int> Interpreter::uint32’ [-fpermissive]
In file included from /src/Interpreter.h:6:0,
                 from /src/main.cpp:3:
/src/types.h:9:28: error: changes meaning of ‘uint32’ from ‘typedef long long unsigned int uint32’ [-fpermissive]
 typedef unsigned long long uint32;
                            ^
In file included from /src/main.cpp:3:0:
/src/Interpreter.h:30:68: error: expected unqualified-id before ‘>’ token
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                                    ^
/src/Interpreter.h:30:54: error: wrong number of template arguments (1, should be 2)
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                      ^
In file included from /src/Interpreter.h:7:0,
                 from /src/main.cpp:3:
/src/Hashmap.h:17:7: error: provided for ‘template<class K, class V> class Hashmap’
 class Hashmap {
       ^
/src/main.cpp: In function ‘int main(int, char**)’:
/src/main.cpp:6:36: error: use of deleted function ‘Interpreter::Interpreter()’
     auto interpreter = Interpreter();
                                    ^
In file included from /src/main.cpp:3:0:
/src/Interpreter.h:23:7: note: ‘Interpreter::Interpreter()’ is implicitly deleted because the default definition would be ill-formed:
 class Interpreter {
       ^
/src/Interpreter.h:30:62: note: initializer for ‘Hashmap<long long unsigned int, long long unsigned int>* Interpreter::jumpTable’ is invalid
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                              ^
/src/Interpreter.h:23:7: error: no matching function for call to ‘Hashmap<long long unsigned int, long long unsigned int>::Hashmap()’
 class Interpreter {
       ^
/src/Interpreter.h:23:7: note: candidates are:
In file included from /src/Interpreter.h:7:0,
                 from /src/main.cpp:3:
/src/Hashmap.h:71:14: note: Hashmap<K, V>::Hashmap(uint32, float) [with K = long long unsigned int; V = long long unsigned int; uint32 = long long unsigned int]
     explicit Hashmap(uint32 size, float loadFactor = 0.75f) {
              ^
/src/Hashmap.h:71:14: note:   candidate expects 2 arguments, 0 provided
/src/Hashmap.h:17:7: note: constexpr Hashmap<long long unsigned int, long long unsigned int>::Hashmap(const Hashmap<long long unsigned int, long long unsigned int>&)
 class Hashmap {
       ^
/src/Hashmap.h:17:7: note:   candidate expects 1 argument, 0 provided
/src/Hashmap.h:17:7: note: constexpr Hashmap<long long unsigned int, long long unsigned int>::Hashmap(Hashmap<long long unsigned int, long long unsigned int>&&)
/src/Hashmap.h:17:7: note:   candidate expects 1 argument, 0 provided
In file included from /test/test_interpreter.cpp:3:0:
/test/../src/Interpreter.h:30:62: error: expected ‘;’ at end of member declaration
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                              ^
/test/../src/Interpreter.h:30:62: error: declaration of ‘Hashmap<long long unsigned int, long long unsigned int> Interpreter::uint32’ [-fpermissive]
In file included from /test/../src/Interpreter.h:6:0,
                 from /test/test_interpreter.cpp:3:
/test/../src/types.h:9:28: error: changes meaning of ‘uint32’ from ‘typedef long long unsigned int uint32’ [-fpermissive]
 typedef unsigned long long uint32;
                            ^
In file included from /test/test_interpreter.cpp:3:0:
/test/../src/Interpreter.h:30:68: error: expected unqualified-id before ‘>’ token
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                                    ^
/test/../src/Interpreter.h:30:54: error: wrong number of template arguments (1, should be 2)
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                      ^
In file included from /test/../src/Interpreter.h:7:0,
                 from /test/test_interpreter.cpp:3:
/test/../src/Hashmap.h:17:7: error: provided for ‘template<class K, class V> class Hashmap’
 class Hashmap {
       ^
/test/test_interpreter.cpp: In member function ‘virtual void Test_testZeroFilledMemory_Test::TestBody()’:
/test/test_interpreter.cpp:10:36: error: use of deleted function ‘Interpreter::Interpreter()’
     auto interpreter = Interpreter();
                                    ^
In file included from /test/test_interpreter.cpp:3:0:
/test/../src/Interpreter.h:23:7: note: ‘Interpreter::Interpreter()’ is implicitly deleted because the default definition would be ill-formed:
 class Interpreter {
       ^
/test/../src/Interpreter.h:30:62: note: initializer for ‘Hashmap<long long unsigned int, long long unsigned int>* Interpreter::jumpTable’ is invalid
     Hashmap<uint32, uint32>* jumpTable = new Hashmap<uint32, uint32>(256); // this for some reason does not compile with new Hashmap<uint32, uint32>()
                                                              ^
/test/../src/Interpreter.h:23:7: error: no matching function for call to ‘Hashmap<long long unsigned int, long long unsigned int>::Hashmap()’
 class Interpreter {
       ^
/test/../src/Interpreter.h:23:7: note: candidates are:
In file included from /test/../src/Interpreter.h:7:0,
                 from /test/test_interpreter.cpp:3:
/test/../src/Hashmap.h:71:14: note: Hashmap<K, V>::Hashmap(uint32, float) [with K = long long unsigned int; V = long long unsigned int; uint32 = long long unsigned int]
     explicit Hashmap(uint32 size, float loadFactor = 0.75f) {
              ^
/test/../src/Hashmap.h:71:14: note:   candidate expects 2 arguments, 0 provided
/test/../src/Hashmap.h:17:7: note: constexpr Hashmap<long long unsigned int, long long unsigned int>::Hashmap(const Hashmap<long long unsigned int, long long unsigned int>&)
 class Hashmap {
       ^
/test/../src/Hashmap.h:17:7: note:   candidate expects 1 argument, 0 provided
/test/../src/Hashmap.h:17:7: note: constexpr Hashmap<long long unsigned int, long long unsigned int>::Hashmap(Hashmap<long long unsigned int, long long unsigned int>&&)
/test/../src/Hashmap.h:17:7: note:   candidate expects 1 argument, 0 provided
/test/test_interpreter.cpp: In member function ‘virtual void Test_testIncrementMemory_Test::TestBody()’:
/test/test_interpreter.cpp:17:36: error: use of deleted function ‘Interpreter::Interpreter()’
     auto interpreter = Interpreter();
                                    ^
/test/test_interpreter.cpp: In member function ‘virtual void Test_testDecrementMemory_Test::TestBody()’:
/test/test_interpreter.cpp:25:36: error: use of deleted function ‘Interpreter::Interpreter()’
     auto interpreter = Interpreter();
                                    ^
/test/test_interpreter.cpp: In member function ‘virtual void Test_testOutputBuffer_Test::TestBody()’:
/test/test_interpreter.cpp:33:36: error: use of deleted function ‘Interpreter::Interpreter()’
     auto interpreter = Interpreter();
                                    ^
/test/test_interpreter.cpp: In member function ‘virtual void Test_testMultiplication_Test::TestBody()’:
/test/test_interpreter.cpp:40:36: error: use of deleted function ‘Interpreter::Interpreter()’
     auto interpreter = Interpreter();
                                    ^
/test/test_interpreter.cpp: In member function ‘virtual void Test_testHelloWorld_Test::TestBody()’:
/test/test_interpreter.cpp:49:36: error: use of deleted function ‘Interpreter::Interpreter()’
     auto interpreter = Interpreter();
                                    ^
/test/test_interpreter.cpp: In member function ‘virtual void Test_testHelloWorld2_Test::TestBody()’:
/test/test_interpreter.cpp:57:36: error: use of deleted function ‘Interpreter::Interpreter()’
     auto interpreter = Interpreter();

Aucun commentaire:

Enregistrer un commentaire