logo
down
shadow

Is there a signed `sizeof` alternative in Qt


Is there a signed `sizeof` alternative in Qt

By : AchenDajiangyou
Date : November 20 2020, 04:01 AM
it helps some times Sizes of Qt containers return (signed) int. And we know in Qt, a lot more is using int instead of the unsigned type size_t in order to do arithmetic operations without the need for casting. See Why does Qt use a signed int type for its container classes & Why QVector.size() returns int , You can make your own, safe version:
code :
template<std::size_t s>
constexpr int safeIntCast()
{
    static_assert(s <= std::numeric_limits<int>::max(), "Type too large for ssizeof()!");
    return static_cast<int>(s);
}

#define ssizeof(x) safeIntCast<sizeof(x)>()

/// Usage

static_assert(ssizeof(int) == 4);
static_assert(ssizeof(1.0) == 8);

static_assert(ssizeof(std::declval<int>()) == 4);
static_assert(ssizeof(int[1000000000000]) > 4); // Conversion problem is caught!


Share : facebook icon twitter icon
C# sizeof(enum) alternative? (to workaround resharper false error)?

C# sizeof(enum) alternative? (to workaround resharper false error)?


By : Chris Adzima
Date : March 29 2020, 07:55 AM
With these it helps In C# I've got some "safe" API code related to UAC elevation. It involves getting the size of an enum (as follows) , Looks ugly, but may work:
code :
int myEnumSize = Marshal.SizeOf(Enum.GetUnderlyingType(typeof(MyEnum)));
enum Enum1 : sbyte { A, B, C, D }
enum Enum2 : short { A, B, C, D }
enum Enum3 : int { A, B, C, D }
enum Enum4 : long { A, B, C, D }

enum Enum5 : byte { A, B, C, D }
enum Enum6 : ushort { A, B, C, D }
enum Enum7 : uint { A, B, C, D }
enum Enum8 : ulong { A, B, C, D }
Can't print the sizeof a signed int type

Can't print the sizeof a signed int type


By : Aaron Covrig
Date : March 29 2020, 07:55 AM
I hope this helps . Why when I try to do this: , This is the correct way:
code :
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of int: %lu bytes\n", (unsigned long) sizeof(int));
Is rightshifting signed int by 8 * sizeof(int) or more undefined?

Is rightshifting signed int by 8 * sizeof(int) or more undefined?


By : Charlie Elbakry
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , sizeof (int) is the size of int in bytes, so it's not relevant. What's relevant is not the size, but the width, which is the number of value bits in the representation (plus the sign bit for signed types).
If the right operand of a << or >> operator is greater than or equal to the width of the promoted left operand, the behavior is undefined. (For example, if the left operand is of type short, it's promoted to int before the operation is applied).
alternative to sizeof that ignores the check for is_countable

alternative to sizeof that ignores the check for is_countable


By : Sourabh
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , If you want a function that checks sizeof() and ignores uncountable parameters, you could define the following in your project's header:
code :
function countableSizeof($obj) {
  if(!is_countable($obj)) {
    return 0; //or a sentinel value of your choosing
  }
  return sizeof($obj);
}
Where in the C++ Standard does it say that sizeof(wchar_t) <= sizeof(long) and sizeof(bool) <= sizeof(long)?

Where in the C++ Standard does it say that sizeof(wchar_t) <= sizeof(long) and sizeof(bool) <= sizeof(long)?


By : Koco
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further It's hard to imagine a sizeof(wchar_t)>N && sizeof(long)<=N implementation for any value of N, let alone one on which anyone would consider using wchar_t. That said:
In fact, I think it's wrong. 4.5 "Integral promotions" p2 explicitly allows for the possibility:
Related Posts Related Posts :
  • C++ on Vistual Studio with CMake error: manifest 'build.ninja' still dirty after 100 tries
  • Is there a way to automatically resolve an overloaded method via a template parameter?
  • Fastest way to find pair in a vector, remove it while iterating
  • error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR'
  • Unable to call boost::clear_vertex while using listS for the vertex and edge lists
  • Decoding binary data from serial port
  • Pattern to Implement an OO interface to a C program written in an imperative style
  • CEPH + Librados++ is not returning consisten results
  • `LoadLibraryExW` triggers exception `0xC0000023` from `NtMapViewOfSection`
  • static_cast to a struct type to access all of its member variable
  • Forward declaration for boost::intrusive_ptr class to reduce compile time
  • How to use priority_queue with a non-static compare method of class instance?
  • Template parameters inside and outside class/struct
  • Determining prime number
  • How to resolve ambiguity between constructors taking std::string and std::vector
  • My program crashes when I try to change private values from an object
  • Unordered_map with custom class as key
  • Strict aliasing rules broken with templates and inheritance
  • C++ Derived Class Override Return Type
  • singly linked list c++ constructor, destructor and printing out
  • How to clone class with vector of unique_ptr to base class
  • error: no match for operator
  • std::vector doesnt accept my struct as template
  • selection of people's contours
  • how to fix the (Error using mexOpenCV) on matlab?
  • Is or was there a proposal for c++ to use the context for short enum values?
  • Fair assumptions about std::hash implementations
  • undefined reference to libusb using cyusb
  • Function returns null pointer instead of address
  • C++17 copy elision and object destruction
  • Input multiple strings via operator>> c++
  • Avoiding overflow boost container
  • How to Write a Lambda Wrapping a Function with Optional Return Value
  • Partial specialization with more template parameters
  • How to convert fixed size array to pointer on pointer array
  • Memory leak in const member constructor with tag dispatching
  • C++ function with a generic and optional class
  • Custom QGraphicsItem That Contains Child QGraphicsItems
  • Are There Restrictions on What can be Passed to auto Template Parameters?
  • Rotating line inside rectangle bounds
  • Why do I need dynamic memory allocation if I can just create an array?
  • How can I convert a text file into a form that MPI_Bcast can send?
  • How to get array of all `this` of an instance
  • Using pointers as parameters
  • Automatic type deduction with const_cast is not working
  • Why does std::is_rvalue_reference not do what it is advertised to do?
  • Function Template Specialization with Forward Declared Type
  • template deduction failed in vector
  • clarification on overloading the -> operator
  • What is __m128d?
  • QtConcurrent: why releaseThread and reserveThread cause deadlock?
  • Function receiving different value than passed
  • Can C++ close a '''fstream''' variable after '''.close()'''?
  • Is it necessary to overload operator in this specific case
  • Comparing an element of a string array with a string
  • how to dereference a pointer of a map of pointers to objects in c++
  • How recursive function control flow change for static variable?
  • SDL 2 blitting BMP File
  • Why does an extremely large value cause this code to repeat infinitely?
  • Function returning different value than what is in function body before return statement
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org