Is it possible to use a class template in its member functions? - c++

My class is size parameterized. In one of its methods I have to create a temporary array, but I don't know how to pass the class's size template to the member function. This is how I tried:
#include <array>
template<unsigned int N>
class MyClass{
std::array<int,N> m_data;
public:
void myFunc(){
std::array<int,N> tempArray;
}
};
int main(){
MyClass<5> obj;
obj.myFunc();
}
Edit:
Build log:
C:\Windows\system32\cmd.exe /C ""C:/Program Files/mingw-w64/x86_64-6.3.0-win32-seh-rt_v5-rev1/mingw64/bin/mingw32-make.exe" -j6 SHELL=cmd.exe -e -f Makefile"
"----------Building project:[ hatizsak_konyv - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv'
"C:/Program Files/mingw-w64/x86_64-6.3.0-win32-seh-rt_v5-rev1/mingw64/bin/g++.exe" -c "E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv/main.cpp" -g -O0 -Wall -o ./Debug/main.cpp.o -I. -I.
E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv/main.cpp: In instantiation of 'void MyClass<N>::myFunc() [with unsigned int N = 5u]':
E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv/main.cpp:15:16: required from here
E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv/main.cpp:8:27: warning: unused variable 'tempArray' [-Wunused-variable]
std::array<int,N> tempArray;
^~~~~~~~~
"C:/Program Files/mingw-w64/x86_64-6.3.0-win32-seh-rt_v5-rev1/mingw64/bin/g++.exe" -o ./Debug/hatizsak_konyv #"hatizsak_konyv.txt" -L.
mingw32-make.exe[1]: Leaving directory 'E:/progi/c++/CodeLite/Other/algoritmusok/dinamikus_programozas/hatizsak_konyv'
====1 errors, 1 warnings====

Template parameters are visible inside methods of the template class; the code is correct.
There's no error at all, nor in the provided code example, nor in the build log. The message in the build log is just a warning (with the lines before providing context for it), which correctly warns you about the fact that that variable isn't used, as per the -Wall option provided on the command line. Other than that, the code compiles fine, both on ideone and on my machine (where it gives you exact same warning, not error).
[matteo#teolapkubuntu /tmp]$ g++ -Wall -Wextra -std=c++11 stuff.cpp
stuff.cpp: In instantiation of ‘void MyClass<N>::myFunc() [with unsigned int N = 5u]’:
stuff.cpp:15:16: required from here
stuff.cpp:8:27: warning: unused variable ‘tempArray’ [-Wunused-variable]
std::array<int,N> tempArray;
^~~~~~~~~
The "1 error" message at the end of the build log is just CodeLite misinterpreting the compiler output; there is an open bug about it, with conditions similar to yours.

Related

Why does C allow me to call an undeclared function? [duplicate]

This question already has answers here:
Why the error of - calling the function before being declared, is not shown?
(3 answers)
Closed 4 years ago.
I have two files: test1.c, and test2.c, which contains the main() function.
test1.c:
#include <stdio.h> // printf() function declaration/prototype
// function definition
void say_hello() {
printf("\tHello, world!\n");
}
test2.c:
#include <stdio.h> // printf() function declaration/prototype
int main() {
printf("Inside main()\n");
say_hello();
return 0;
}
And this is my makefile:
a.out: test1.o test2.o
$(CXX) -o a.out test1.o test2.o
test1.o: test1.c
$(CXX) -c test1.c
test2.o: test2.c
$(CXX) -c test2.c
Now it should be clear where the problem lies: The main() function in test2.c calls say_hello() without declaring it!
I run the following command, to use the gcc compiler:
make CXX=gcc
I get this warning to the screen:
gcc -c test1.c
gcc -c test2.c
test2.c: In function ‘main’:
test2.c:16:3: warning: implicit declaration of function ‘say_hello’ [-Wimplicit-function-declaration]
say_hello();
^
gcc -o a.out test1.o test2.o
Although the *.o files got compiled and linked into the executable. That's weird. Imagine my surprise when I run the a.out file, and I see that main() successfully called say_hello(), a function which is not declared inside of main's translation unit, as if there were no issue at all! I reason that since say_hello() was not previously declared in test2.c, it should not allowed to be called by main() at all. Notice that I've added comments to the #include <stdio.h>. These preprocessor directives include the function declarations/prototypes, which extend their scope into the corresponding *.c files. That is why we are able to use them. No function declaration/prototype == no scope in that translation unit, or so I thought until now.
Then I compiled the above code as C++ code:
make CXX=g++
I get this error to the screen:
test2.c: In function ‘int main()’:
test2.c:16:13: error: ‘say_hello’ was not declared in this scope
say_hello();
^
makefile:18: recipe for target 'test2.o' failed
make: *** [test2.o] Error 1
g++ does what it's supposed to do, and stops the compilation process. But gcc did not do this! What's going on? Is it a perk of the C programming language? Is it an issue with the compiler?
Simple, because C allows undeclared functions to be called and C++ does not. Either way, gcc warns you and you may want to take warnings seriously.

Variadic template iteration and GCC

I've implemented a variadic template function according to wiki. And iterate it with help of "overloading with "termination versions" of functions". The code:
void writeValue(QDataStream& /*data*/) {}
template<typename A, typename... Values>
void writeValue(QDataStream& data, const A& arg1, const Values&... args)
{
data << arg1;
writeValue(data, args...);
}
template<typename... Values>
quint32 PrepareMessage(QDataStream& data, func_code fcode, Values... parameters)
{
data << quint32(fcode);
writeValue(data, parameters...);
return 0;
}
It can be build and used without any problems with Qt 5.5 for Windows over MSVC2013 64bit toolchain.
Now I'm trying to build the same code on Linux with Qt 5.5 for Linux over GCC 64 bit and getting following errors at compilation:
g++ -c -pipe -std=c++11 -g -Wall -W -D_REENTRANT -fPIC -D_64bit -DQT_NETWORK_LIB -DQT_CORE_LIB -I../Trans2QuikWrapper -I. -I../../../Qt/5.5/gcc_64/include -I../../../Qt/5.5/gcc_64/include/QtNetwork -I../../../Qt/5.5/gcc_64/include/QtCore -I. -I../../../Qt/5.5/gcc_64/mkspecs/linux-g++ -o moc_T2Q_Client.o moc_T2Q_Client.cpp
g++ -Wl,-rpath,/home/truf/Qt/5.5/gcc_64 -Wl,-rpath,/home/truf/Qt/5.5/gcc_64/lib -o t2q T2Q_Client.o main_client.o moc_T2Q_Client.o -L/home/truf/Qt/5.5/gcc_64/lib -lQt5Network -lQt5Core -lpthread
main_client.o: In function `int QGenericAtomicOps<QBasicAtomicOps<4> >::load<int>(int const&)':
/home/truf/.wine/drive_c/build-t2q-Desktop_Qt_5_5_1_GCC_64bit-Debug/../Trans2QuikWrapper/io_utils.h:16: multiple definition of `writeValue(QDataStream&)'
Makefile:192: recipe for target 't2q' failed
T2Q_Client.o:/home/truf/.wine/drive_c/build-t2q-Desktop_Qt_5_5_1_GCC_64bit-Debug/../Trans2QuikWrapper/io_utils.h:16: first defined here
moc_T2Q_Client.o: In function `int QGenericAtomicOps<QBasicAtomicOps<4> >::load<int>(int const&)':
/home/truf/.wine/drive_c/build-t2q-Desktop_Qt_5_5_1_GCC_64bit-Debug/../Trans2QuikWrapper/io_utils.h:16: multiple definition of `writeValue(QDataStream&)'
T2Q_Client.o:/home/truf/.wine/drive_c/build-t2q-Desktop_Qt_5_5_1_GCC_64bit-Debug/../Trans2QuikWrapper/io_utils.h:16: first defined here
collect2: error: ld returned 1 exit status
make: *** [t2q] Error 1
gcc version is 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2). Wine is not involved - just a folder location.
Are variadic templates supported there? Any additional compilation parameters needed? Or roblem is in code?
The error message says:
multiple definition of `writeValue(QDataStream&)'
To solve this, you must declare writeValue() as inline:
inline void writeValue(QDataStream& /*data*/) {}
When you define a function in a header file, you always should mark it as inline. This way, it won't break the ODR if you include the header in multiple translation units.

STL push_back optimize cause array subscript is above array bounds

Test environment:
CentOS 7.0 g++ 4.8.2
Arch Linux g++ 4.9.0 20140604 (prerelease)
Arch Linux g++ 4.9.1
Compile command cases:
PASS: g++ -Wall t.cpp
FAIL: g++ -Wall -O2 t.cpp
PASS: g++ -Wall -O2 t.cpp # and replace 2 with 3 on line 13
PASS: g++ -Wall -O2 t.cpp # and comment out line 14
PASS: g++ -Wall -O2 --std=c++11 t.cpp # for g++ 4.8/4.9
The FAIL message:
t.cpp: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vecto
<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = Object; _Alloc = std::allocator<Ob
ject>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<Object*, s
td::vector<Object> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = Object*]’
t.cpp:17:15: warning: array subscript is above array bounds [-Warray-bounds]
~Object() {};
^
t.cpp:17:15: warning: array subscript is above array bounds [-Warray-bounds]
t.cpp
#include <vector>
class TestCls {
public:
TestCls() {};
virtual ~TestCls() {};
};
class TestCls1 : public TestCls
{
};
class Object {
public:
TestCls m_member[2];
TestCls1 m_member1[2]; // LINE 13, if change to [3] it works.
TestCls1 m_member2[2]; // LINE 14, if comment out this line, it works.
Object() {};
~Object() {}; // LINE 17 the warning line
};
class Container {
public:
std::vector<Object> m_obj;
Container() {};
~Container() {};
};
int main() {
Container con;
Object obj;
con.m_obj.push_back(obj);
}
This is a type of bogus warning generated by GCC due to issues with Value Range Propagation (which is the middle-end pass that generates array bounds warnings) interacting with the various loop optimizer passes (such as loop peeling and loop unrolling). As is mentioned in the various bugzillas linked, these represent missed optimization opportunities as well, but the underlying issue (or issues) in VRP have proved elusive for the GCC developers. That said, reporting this occurrence to the GCC Bugzilla is a good idea, especially since you have a MWE at hand.
I found a solution, but I don't know the reason.
// ...
class Object {
public:
// ...
~Object();
};
Object::~Object() {}; // move to outside LINE 19
//...
gcc -Wall enables all compiler's warning messages. This option should
always be used, in order to generate better code.
So, try delete -Wall. Example:
with -Wall http://goo.gl/d4cces
without -Wall http://goo.gl/4vY2Un

Problems with constructor of a class with template

I'm trying to write a container class using C++ and templates. However, I'm having a compilation error I don't understand...
The variable elems is a private vector, declaration is:
private:
vector<DataType> elems;
The vector is a custom vector. Its constructor is:
vector::vector(int init_capacity) : vect_capacity(init_capacity), vect_size(0), vect_elems(NULL){
assert(init_capacity >= 0);
if (init_capacity > 0){
vect_elems = new Object[init_capacity];
}
}
The constructor can be seen below:
template <class DataType>
bag<DataType>::bag(int init_capacity) : elems(init_capacity) {
}
This code returns the following error:
../src/vector.h: In instantiation of ‘vector<DataType>::vector(int) [with DataType = int]’:
../src/bag.h:33:60: required from ‘bag<DataType>::bag(int) [with DataType = int]’
../src/bag_test.cpp:6:17: required from here
I honestly have no idea what could be possibly happening. Will be immensely grateful to anyone that can point me in the right direction...
Sorry for the very stupid question. It's true that the compiler does complain about this, but the code actually compiles. Thanks to #WhozCraig and #n.m who insisted that this wasn't an error, I noticed that it actually was building. Thanks! For future reference, I do post the whole message:
**** Build of configuration Debug for project ADS ****
make all
Building file: ../src/bag_test.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/bag_test.d" -MT"src/bag_test.d" -o "src/bag_test.o" "../src/bag_test.cpp"
In file included from ../src/bag_test.cpp:2:0:
../src/bag.h:23:66: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const bag<DataType>&)’ declares a non-template function [-Wnon-template-friend]
../src/bag.h:23:66: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
In file included from ../src/bag.h:2:0,
from ../src/bag_test.cpp:2:
../src/vector.h: In instantiation of ‘vector<DataType>::vector(int) [with DataType = int]’:
../src/bag.h:34:53: required from ‘bag<DataType>::bag(int) [with DataType = int]’
../src/bag_test.cpp:6:17: required from here
../src/vector.h:100:6: warning: ‘vector<int>::vect_capacity’ will be initialized after [-Wreorder]
../src/vector.h:99:6: warning: ‘int vector<int>::vect_size’ [-Wreorder]
../src/vector.h:108:1: warning: when initialized here [-Wreorder]
Finished building: ../src/bag_test.cpp
Building target: ADS
Invoking: GCC C++ Linker
g++ -o "ADS" ./src/bag_test.o
Finished building target: ADS
**** Build Finished ****

Forcing GCC 4.x to treat -Wreturn-type as an error without enabling -Werror?

Suppose we have the following code:
#if !defined(__cplusplus)
# error This file should be compiled as C++
#endif
#include <stdio.h>
#include <string>
//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}
std::string GetSomeString()
{
// case #1
}
};
#endif // USE_CXX_CLASS
int foo()
{
// case #2
}
int
main (int argc, char *argv[])
{
(void)argc;
(void)argv;
#ifdef USE_CXX_CLASS
SomeClass someInstance;
someInstance.GetSomeString();
#endif // USE_CXX_CLASS
foo();
return 0;
}
And suppose that it were to be compiled the C++ compiler (and not the C compiler) from GCC version 4.2.1 with the options -Wreturn-type -Werror=return-type. If the above code is compiled as is without first uncommenting the //#define USE_CXX_CLASS line above, then you will see a warning but no error:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function
But if the //#define USE_CXX_CLASS line is uncommented, then the warning is treated as an error:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1
Yes, one is a non-member function (case #2), and the other is a C++ function (case #1). IMO, that should not matter. I want both conditions treated as an error, and I don't want to add -Werror or -Wall at this point in time (probably will do so later, but that is out of scope of this question).
My sub-questions are:
Is there some GCC switch that I am missing that should work? (No I do not want to use #pragma's.)
Is this a bug that has been addressed in a more recent version of GCC?
For reference, I have already poured through other similar questions already, including the following:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
C question: no warning?
Is a return statement mandatory for C++ functions that do not return void?
It has been fixed, it works well with g++ 9.3: both member functions and free functions are treated as error with -Wall -Werror=return-type
I do see an error even w/o the USE_CXX_CLASS flag. i.e. g++ is consistent with the error for both class member functions and non member functions.
g++ (GCC) 4.4.3 20100127 (Red Hat 4.4.3-4)
It seems to me that what you need is a shell script wrapper around gcc.
Name it something like gcc-wrapper and g++-wrapper.
In your Makefile set CC and CXX to the wrappers.
Have the wrapper invoke GCC and pipe its output to another program which will search for your desired warning strings.
Have the search program exit with an error when it finds the warning.