Musings on the C charter

Within the C committee, there’s been a lot of talk about what the C charter means and whether it should be updated or not. My personal musing on the topic is that the C charter needs to be updated in order for C to remain relevant long-term. Whether C should remain relevant long-term is an exercise left to the reader. Note, these thoughts may change over time as I gather more feedback from more places.

Continue reading
Posted in C/C++ | Tagged , | 4 Comments

What’s New in C in 2023?

The year is 2023 and you’re asking “What’s new in C?” Surely the answer is “absolutely nothing”, right?

Wrong!

C23 will be released in the coming months (likely in early 2024), and this is a whirlwind tour of what changes were made for the latest revision of one of the world’s most popular programming languages.

I originally prepared this talk for NDC Tech Town in Kongsberg, but I was unable to give the talk because a hurricane + poor airline services interrupted my travel plans. Oh well, their loss is your gain! I hope you enjoy the talk.

Errata

  • When mentioning that signed overflow is still implementation-defined, I misspoke. I was talking about this situation involving unsigned -> signed conversion:
unsigned int i = UINT_MAX;
signed int j = i; // Implementation-defined behavior

However, signed *overflow* is still undefined behavior, as it always was. e.g.,

signed int i = INT_MAX + 1; // Undefined behavior
Posted in C/C++ | Tagged , | 11 Comments

Don’t use the [[likely]] or [[unlikely]] attributes

C++20 introduced the likelihood attributes [[likely]] and [[unlikely]] as a way for a programmer to give an optimization hint to their implementation that a given code path is more or less likely to be taken. On its face, this seems like a great set of attributes because you can give hints to the optimizer in a way that is hopefully understood by all implementations and will result in faster performance. What’s not to love?

Continue reading
Posted in C/C++ | Tagged , , | 8 Comments

Member Function Ref Qualifiers

One of the lesser-known features of C++11 is the fact that you can overload your non-static member functions based on whether the implicit this object parameter is an lvalue reference or an rvalue reference by specifying a functions ref-qualifier. This feature works similar to the way cv-qualifiers work when specifying a method must be called on a const or volatile object, and can in fact be combined with cv-qualifiers.
Continue reading

Posted in C/C++ | Tagged , , | 2 Comments

Binary Operator Overloading

In C++, there are two forms of binary operator overloading you can use when designing an API. The first form is to overload the operator as a member function of the class, and the second form is to overload the operator as a friend function of the class. I want to explore why you would use one form of overloading instead of the other, using a Fraction class as an example.
Continue reading

Posted in C/C++, Framework Design | Tagged , | 2 Comments

Implementing a four-bit adder with C++ template metaprogramming

I recently read a post by Phillip Larkson where the C preprocessor was used to implement a four-bit adder entirely at compile time. This got me wondering whether I could implement the same concept using C++ template metaprogramming. It seemed theoretically possible as all the components can be calculated at runtime, but I wanted to avoid making use of the preprocessor for anything but supplying the original values to be added. The goal was to compile something on the command line like:

clang -DNUMBER_1=5 -DNUMBER_2=7 foo.cpp

and have the value 12 written out to the command line.
Continue reading

Posted in C/C++ | Tagged | 1 Comment

Interesting Note About the sizeof Operator

The expression used in a sizeof operator is an unevaluated expression in C and C++. This can make for some surprising situations if you are unaware of it. For instance:

#include <stdio.h>

int main( void ) {
  int a = 12;
  int b = sizeof( ++a );
  printf( "%d\n", a );
  return 0;
}

This code will print 12 instead of 13 because the expression ++a is unevaluated.

So what does it mean for an expression to be unevaluated? Basically, it means that the expression is used at compile time only as a way to determine a type, which is then used to evaluate the result of the sizeof operator. So in the above example, a’s type is determined, and the resulting type for ++ is determined, but no code is generated to execute the ++.

Here’s an abusive example that demonstrates this:

#include <stdio.h>

class c {
public:
  double operator++();
};

int main(int argc, char *argv[]){
  c c1;
  int i = sizeof( ++c1 );
  int j = sizeof( c1 );
  ::printf( "%d, %d\n", i, j );
  return 0;
}

If you run this code, you will see 8, 1 printed and will not get a link error (because of the failure to define operator++)!

Posted in C/C++ | Tagged , | 7 Comments

MSVC Pointer Type Attributes

One of the lesser-known features of Visual Studio’s C/C++ compiler are the pointer type attributes __ptr32 and __ptr64. More information about them can be found on MSDN. These pointer type attributes are used to control the visible size and behavior of pointers in 32- and 64-bit applications. Their usage is a bit strange, but if you need to do interop between 32- and 64-bit mode, they can be handy features to have. Additionally, there are the __sptr and __uptr qualifiers which allow you to specify how the pointer types are extended. __sptr denotes sign extension, and __uptr denotes zero extension. Information about these qualifiers can also be found on MSDN.
Continue reading

Posted in C/C++, Win32 | 2 Comments

Small Break in the Silence

Just because I’ve not written many posts lately doesn’t mean I’ve been silent. You should go check out the Tidbits page, it currently has over 20 little juicy pieces of information about C and C++. I’ve been using it as a training exercise for my coworkers, and it’s been very successful so far. I’ll likely be continuing with regular Friday updates of Tidbits for quite a while to come.

I do hope to write some more full-length blog posts now that life is starting to settle back down again. So I’m not dormant, honest!

Posted in Uncategorized | Leave a comment

How Variable Argument Lists Work in C

Variable argument lists are very arcane in the world of C. You’ll see them expressed in function signatures as … at the end of the parameter list, but you may not understand how they work or what they do.
Continue reading

Posted in C/C++ | Tagged , , , | 11 Comments