const-cast

Filling a std::array at compile time and possible undefined behaviour with const_cast

笑着哭i 提交于 2019-12-04 07:47:54
It is known that std::array::operator[] since C++14 is constexpr , see declaration below: constexpr const_reference operator[]( size_type pos ) const; However, it is also const qualified. This causes implications if you want to use the subscript operator of a std::array in order to assign values to your array at compile time. For example consider the following user literal: template<typename T, int N> struct FooLiteral { std::array<T, N> arr; constexpr FooLiteral() : arr {} { for(int i(0); i < N; ++i) arr[i] = T{42 + i}; } }; The above code won't compile if you try to declare a constexpr

Is this const_cast undefined behavior?

邮差的信 提交于 2019-12-04 03:11:04
I was wondering whether the following is undefined behavior // Case 1: int *p = 0; int const *q = *const_cast<int const* const*>(&p); // Case 2: (I think this is the same) int *p = 0; int const *const *pp = &p; int const *q = *pp; Is this undefined behavior by reading a int* as if it were a int const* ? I think it is undefined behavior, but I previously thought that only adding const in general is safe, so I'm unsure. Qualification-wise, it's fine. With each expression split into a statement: int *p = 0; // ok int **addrp = &p; // ok int const *const *caddrq = addrp; // ok, qualification conv.

C++ difference between adding const-ness with static_cast and const_cast of “this” object?

一个人想着一个人 提交于 2019-12-03 17:13:16
问题 As per Scott Meyers, to prevent repetition of code in the const version of a getter and the non-const version of a getter, call the const version of the method from the non-const version: static_cast<const A&>(*this).Methodology(); however , in accidental usage due to an overzealous Visual Assist X Intellisense I typed: const_cast<const A&>(*this).Methodology(); and it worked just fine. What are any and all differences in this case with using a particular cast? IDE in use: Visual Studio 2010.

Modifying element of const std::vector<T> via const_cast

隐身守侯 提交于 2019-12-03 16:23:52
问题 Does the following program have undefined behavior? #include <iostream> #include <vector> struct Foo { const std::vector<int> x; }; int main() { std::vector<int> v = {1,2,3}; auto f = new Foo{v}; const_cast<int&>(f->x[1]) = 42; // Legal? std::cout << f->x[1] << "\n"; } Note that it not using const_cast to strip constness from f->x , but instead stripping constness from f->x[x] , which presumably is represented by a separate array. Or is a translation allowed to presume that f->x[1] is

Modifying element of const std::vector<T> via const_cast

一曲冷凌霜 提交于 2019-12-03 05:35:08
Does the following program have undefined behavior? #include <iostream> #include <vector> struct Foo { const std::vector<int> x; }; int main() { std::vector<int> v = {1,2,3}; auto f = new Foo{v}; const_cast<int&>(f->x[1]) = 42; // Legal? std::cout << f->x[1] << "\n"; } Note that it not using const_cast to strip constness from f->x , but instead stripping constness from f->x[x] , which presumably is represented by a separate array. Or is a translation allowed to presume that f->x[1] is immutable after it is created? There is no Undefined Behavior in your example. The above code does not invoke

C++ difference between adding const-ness with static_cast and const_cast of “this” object?

天大地大妈咪最大 提交于 2019-12-03 05:25:41
As per Scott Meyers, to prevent repetition of code in the const version of a getter and the non-const version of a getter, call the const version of the method from the non-const version: static_cast<const A&>(*this).Methodology(); however , in accidental usage due to an overzealous Visual Assist X Intellisense I typed: const_cast<const A&>(*this).Methodology(); and it worked just fine. What are any and all differences in this case with using a particular cast? IDE in use: Visual Studio 2010. Attila Assuming that the type of this is A* , there is no difference. In general const_cast can cast

Where is the undefined behavior when using const_cast<>?

二次信任 提交于 2019-12-01 17:40:39
If I do: const char* const_str = "Some string"; char* str = const_cast<char*>(const_str); // (1) str[0] = "P"; // (2) Where (which line) exactly is the undefined behavior ? I've been searching a lot for this on SO but haven't found any explicit and precise answer (or at least, none that I could understand). Also related: if I use an external library which provides this kind of function: // The documentation states that str will never be modified, just read. void read_string(char* str); Is it ok to write something like: std::string str = "My string"; read_string(const_cast<char*>(str.c_str()));

Where is the undefined behavior when using const_cast<>?

时光怂恿深爱的人放手 提交于 2019-12-01 16:38:45
问题 If I do: const char* const_str = "Some string"; char* str = const_cast<char*>(const_str); // (1) str[0] = "P"; // (2) Where (which line) exactly is the undefined behavior ? I've been searching a lot for this on SO but haven't found any explicit and precise answer (or at least, none that I could understand). Also related: if I use an external library which provides this kind of function: // The documentation states that str will never be modified, just read. void read_string(char* str); Is it

Avoiding const_cast when calling std::set<Type*>::find

帅比萌擦擦* 提交于 2019-12-01 03:25:05
Is there any good way to obviate the const_cast below, while keeping const correctness? Without const_cast the code below doesn't compile. set::find gets a const reference to the set's key type, so in our case it guarantees not to change the passed-in pointer value; however, nothing it guaranteed about not changing what the pointer points to. class C { public: std::set<int*> m_set; bool isPtrInSet(const int* ptr) const { return m_set.find(const_cast<int*>(ptr)) != m_set.end(); } }; Arvid Yes . In C++14, you can use your own comparator that declares int const* as transparent . This would enable

Avoiding const_cast when calling std::set<Type*>::find

不想你离开。 提交于 2019-11-30 23:25:12
问题 Is there any good way to obviate the const_cast below, while keeping const correctness? Without const_cast the code below doesn't compile. set::find gets a const reference to the set's key type, so in our case it guarantees not to change the passed-in pointer value; however, nothing it guaranteed about not changing what the pointer points to. class C { public: std::set<int*> m_set; bool isPtrInSet(const int* ptr) const { return m_set.find(const_cast<int*>(ptr)) != m_set.end(); } }; 回答1: Yes .