const-correctness

Can I pass a const char* array to execv?

旧街凉风 提交于 2019-12-07 04:56:10
问题 This is the prototype for execv : int execv(const char *path, char *const argv[]); Can I pass an array of const char pointers as the second argument? This example program gives a warning when USE_CAST is not set: #include <unistd.h> int main(int argc, char *argv[]) { if (argc > 0) { const char *exe_name = "/bin/echo", *message = "You ran"; const char *exe_args[] = { exe_name, message, argv[0], NULL }; #ifdef USE_CAST execv("/bin/echo", (char **) exe_args); #else execv("/bin/echo", exe_args);

Qt - QList const correctness

本小妞迷上赌 提交于 2019-12-07 02:54:53
问题 A QList<T *> can't easily be const-correct. Consider the function void f(QList<T *> list) { list[0]->constFunction(); } I can change f to void f(QList<const T *> list) but then I can't do f(QList<T *>()); //Compile error anymore, since the compiler can't implicitely cast QList<T *> to QList<const T *> . However, I can explicitely reinterpret-cast the QList as follows: template <typename T> inline QList<const T *> &constList(const QList<T *> &list) { return (QList<const T *> &)list; } This

Const correctness in C# with rich types

大城市里の小女人 提交于 2019-12-06 22:20:15
问题 Coming from a C++ background and trying to learn C#, one of the most frustrating language omissions that I've come across is an equivalent to the const keyword. So, I have been attempting to settle on a pattern that I can use to achieve const correctness in C#. This answer has an interesting suggestion: create a read only interface for all of your types. But as Matt Cruikshank pointed out in the comments, this becomes problematic if your class has collections or other rich types. Particularly

Cannot apply const to typedef reference

落花浮王杯 提交于 2019-12-05 19:11:55
The following code works when applying const to a return value reference of value_type& but errors if I use a typedef of the same type. As an example: class T { }; class A { public: typedef T value_type; typedef value_type& reference; // Not working const reference operator*() const; // But this works? //const value_type& operator*() const; }; // Error! const typename A::reference A::operator*() const { } int main() { return 0; } g++ will error with: 'const' qualifiers cannot be applied My actual code uses templates but I've removed for the example and substituted class T instead. This has no

Const-correctness and hardware writes

本秂侑毒 提交于 2019-12-05 08:24:29
Say I have the following member function: void CFoo::regWrite( int addr, int data ) { reg_write( addr, data ); // driver call to e.g. write a firmware register } Clearly, calling this function doesn't modify the internal state of the object it is called on. However, it changes the state of whatever this Foo instance represents . In circumstances such as these, should Foo::regWrite(int addr, int data) be a const function? You have to decide what the meaning is of "logically const" for the class CFoo , and that depends what the class is for. If CFoo is construed as referring to some data , then

boost::optional not letting me reassign const value types

让人想犯罪 __ 提交于 2019-12-05 08:20:05
It seems to me there should be four variants of boost::optional optional<Foo> => holds a mutable Foo and can be reassigned after initialization optional<Foo const> const => holds a const Foo and can't be reassigned after initialization optional<Foo> const => (should?) hold a mutable Foo but can't be reassigned after initialization optional<Foo const> => (should?) hold a const Foo and can be reassigned after initialization The first 2 cases work as expected. But the optional<Foo> const dereferences to a const Foo, and the optional<Foo const> doesn't allow reassignment after initialization (as

Qt - QList const correctness

亡梦爱人 提交于 2019-12-05 06:35:24
A QList<T *> can't easily be const-correct. Consider the function void f(QList<T *> list) { list[0]->constFunction(); } I can change f to void f(QList<const T *> list) but then I can't do f(QList<T *>()); //Compile error anymore, since the compiler can't implicitely cast QList<T *> to QList<const T *> . However, I can explicitely reinterpret-cast the QList as follows: template <typename T> inline QList<const T *> &constList(const QList<T *> &list) { return (QList<const T *> &)list; } This enables me to use the constList template function to cast any QList<T *> into a QList<const T *> , as in f

How do I require const_iterator semantics in a template function signature?

人盡茶涼 提交于 2019-12-05 05:31:30
I am creating a constructor that will take a pair of input iterators. I want the method signature to have compile-time const semantics similar to: DataObject::DataObject(const char *begin, const char *end) However, I can't find any examples of this. For example, my STL implementation's range constructor for vector is defined as: template<class InputIterator> vector::vector(InputIterator first, InputIterator last) { construct(first, last, iterator_category(first)); } which has no compile-time const guarantees. iterator_category / iterator_traits<> contain nothing relating to const , either. Is

Const correctness in C# with rich types

我的未来我决定 提交于 2019-12-05 03:21:35
Coming from a C++ background and trying to learn C#, one of the most frustrating language omissions that I've come across is an equivalent to the const keyword. So, I have been attempting to settle on a pattern that I can use to achieve const correctness in C#. This answer has an interesting suggestion: create a read only interface for all of your types. But as Matt Cruikshank pointed out in the comments, this becomes problematic if your class has collections or other rich types. Particularly if you do not have control over the type, and can't make it implement a read-only interface. Do any

Is it a good practice to free memory via a pointer-to-const

六眼飞鱼酱① 提交于 2019-12-05 01:38:45
There are many questions discussing the details of C and C++ dealing with pointer-to-const deletion, namely that free() does not accept them and that delete and delete[] do and that constness doesn't prevent object destruction. What I am interested on is whether you think it is a good practice to do so, not what the languages (C and C++) allow. Arguments for pointer-to-const deletion include: Linus Torvalds' kfree() , unlike C's free() , takes a void const* argument because he thinks that freeing the memory does not affect what is pointed to. free() was designed before the introduction of the