LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   New to C++ equivalent Visual Express error detection in g++/gdb ? (https://www.linuxquestions.org/questions/programming-9/new-to-c-equivalent-visual-express-error-detection-in-g-gdb-4175452459/)

dander062 03-02-2013 10:38 PM

New to C++ equivalent Visual Express error detection in g++/gdb ?
 
MS Visual Express C++ catches goofs such as:

int CLASSA = 80.00; //Cost per A Class ticket.

warning: initalizing: conversion from double to int, possible loss of data


I am looking to have the same thing using g++ but have not seen it in man.

NevemTeve 03-02-2013 11:46 PM

Couldn't test it at the moment, but here's some compiler flags you can try: -W -Wall -Wextra -pedantic

ntubski 03-03-2013 10:01 AM

You need -Wconversion, although it doesn't warn for 80.00 because there is no possible loss of precision there. It does for warn for 80.01.

dander062 03-03-2013 11:21 AM

I had been trying -Werror -Wall -Wextra

Thank you.
-Wconversion is what I was looking for. Just too bad it waits until 80.01 instead of 80.00.

I will have to get better at proofreading.

Sergei Steshenko 03-05-2013 01:12 PM

Quote:

Originally Posted by dander062 (Post 4903766)
I had been trying -Werror -Wall -Wextra

Thank you.
-Wconversion is what I was looking for. Just too bad it waits until 80.01 instead of 80.00.

I will have to get better at proofreading.

I am working (was aided by this forum) on what I call strict C++ types. At the moment:


Code:

sergei@amdam2:~/junk/c++_work/strict_types> cat -n main.cxx
    1  #include "Strict_types.hxx"
    2  #include <iostream>
    3
    4
    5  int main()
    6    {
    7    Strict_unsigned uu = 80.0;
    8    // Strict_unsigned u = 1; // error: ?Strict_unsigned::Strict_unsigned(int)? is private
    9    Strict_unsigned u1 = 1u;
    10
    11    std::cerr << "sizeof(Strict_unsigned)=" << sizeof(Strict_unsigned) << "\n";
    12
    13    std::cerr << "u1=" << u1.get() << "\n";
    14
    15    Strict_unsigned u2 = 2u;
    16
    17    u1 = u1 << u2; std::cerr << "u1=" << u1.get() << "\n";
    18
    19    u1 = 16u; std::cerr << "u1=" << u1.get() << "\n";
    20
    21    u1 >>= 2u; std::cerr << "u1=" << u1.get() << "\n";
    22
    23    u1 = u1 << 2u; std::cerr << "u1=" << u1.get() << "\n";
    24
    25    u1 *= (Strict_unsigned)2u; std::cerr << "u1=" << u1.get() << "\n";
    26
    27    return 0;
    28    }
sergei@amdam2:~/junk/c++_work/strict_types> ~/AFSWD/install/gcc-4.5.4/binsh/g++ -Wall -Wextra main.cxx -o main
Strict_types.hxx: In function ‘int main()’:
Strict_types.hxx:2882:5: error: ‘Strict_unsigned::Strict_unsigned(double)’ is private
main.cxx:7:24: error: within this context
sergei@amdam2:~/junk/c++_work/strict_types>

.

I.e. my goal is to be able to avoid exactly errors of such kind.


All times are GMT -5. The time now is 08:10 AM.