C++ Logo


Advanced search

Two small ideas

From: Bill Kerney <bkerney_at_[hidden]>
Date: Wed, 8 Dec 2021 00:43:03 +0000
Hi all,

Two small ideas -

1) Allow this...

void print_matrix(const vector<vector<auto>> &mat) {

...as a more constrained version of auto parameters so that it would work with any vector of vectors, but not with just any type.

2) IMO, this should not compile:

string s = 0;

You can't set a string to any other integer, but since 0 is the same thing as NULL, and strings take char *'s as a parameter, it turns a simple slip of the finger (it's second nature to type int x = 0;) into a guaranteed seg fault. I've also had it happen in templated code, when someone substituted a string and duck typing didn't catch it since 0 can be assigned to a string.

I'm not sure what the benefit of allowing this even is, initializing a string to a null pointer is UB, and this seems to be a pretty open hole in the otherwise-strict type system in C++. It is literally the only way I've segfaulted in C++ since switching over to the modern C++ way of coding things.

Bill Kerney
Clovis Community College

Received on 2021-12-07 18:43:06