Negative number detection

I have a code in which the user must pass> 0 number, otherwise this code will throw. Using the type of this argument as std :: size_t does not work because negative numbers give large positive numbers. Is it good practice if I use a signed type or are there other ways to enforce it?

void f(std::size_t number)
{
//if -1 is passed I'm (for obvious reason) get large positive number
}
+3
source share
9 answers

, , . int, , , , std:: size_t - , , -, .

. size_t, API.

" > 0" , ala

template <typename T>
void f(T t)
{
    if (!(t > 0))
        throw std::runtime_error("be positive!");

    // do stuff with t, knowing it not -1 shoehorned into size_t...
    ...
}

, :

// call me please e.g. f(size_t(10));
void f(size_t);

// unimplemented (and private if possible)... 
// "want to make sure you realise this is unsigned: call f(size_t) explicitly
void f(int32_t);
void f(int64_t);

... , re caller, size_t ( , ). size_t- - , .

- , ( , size_t). , .

EDIT - ...

#include <iostream>                                                             

template <typename T>                                                           
void f(T t)                                                                     
{                                                                               
    if (!(t > 0))                                                               
        std::cout << "bad call f(" << (int)t << ")\n";                               
    else                                                                        
        std::cout << "good f(" << (int)t << ")\n";                                   
}                                                                               

int main()                                                                      
{                                                                               
    f((char)-1);
    f((unsigned char)255);                                                     
}
+2

, . :

, , . , , .

, , , : , ?

+4

, : - unsigned int

, , .

template <class T>
T getNumberInput(std::string prompt, T min, T max) {
    std::string input;
    T value;

    while (true) {
        try {
            std::cout << prompt;
            std::cin.clear();
            std::getline(std::cin, input);
            std::stringstream sstream(input);

            if (input.empty()) {
                throw EmptyInput<std::string>(input);
            } else if (input[0] == '-' && std::numeric_limits<T>::min() == 0) {
                throw InvalidInput<std::string>(input);
            } else if ((sstream >> value) && (value >= min) && (value <= max)) {
                std::cout << std::endl;
                return value;
            } else {
                throw InvalidInput<std::string>(input);
            }
        } catch (EmptyInput<std::string> & emptyInput) {
            std::cout << "O campo não pode ser vazio!\n" << std::endl;
        } catch (InvalidInput<std::string> & invalidInput){
            std::cout << "Tipo de dados invãlido!\n" << std::endl;
        }
    }
}
+2

number std::ptrdiff_t ( ).
, SafeInt f - : void f( SafeInt< std::size_t > i );, , - f( -1 );.

+1

, read-function , int .

EDIT: Ok int , string

0

Fisrt

void f(std::ptrdiff_t number) {
   if (number < 0) throw;
}

void f(std::size_t number) {
   if (number > std::numeric_limits<std::size_t>::max()/2) throw;
}
0

, . , , .

0

, :

void f(std::size_t number)
{
   if(number & (0x1L << (sizeof(std::size_t) * 8 - 1)) != 0)
   {
       // high bit is set. either someone passed in a negative value,
       // or a very large size that we'll assume is invalid.

       // error case goes here
   }
   else
   {
       // valid value
   }
}

8- . =)

, , , , .

API? , , . =) , "" , size_t, , .

0

, .

, , . , , , , . IOW, .

, , .

0

Source: https://habr.com/ru/post/1774255/


All Articles