std

Is it a good practice to overload math functions in namespace std in c++

谁说胖子不能爱 提交于 2019-12-23 09:38:51
问题 I am writing a C++ class which represents an arithmetic type (a c++ wrapper around mpfr), and I'd like to support some functions found in <cmath> (I'll take std::sqrt as an example). So I have the following class: namespace ns { class MyClass { /* ... */ public: friend MyClass sqrt(const MyClass& mc); }; } And I can use it this way: MyClass c; /* ... */ MyClass d = ns::sqrt(c); MyClass e = sqrt(c); // Apparently I don't have to specify ns:: But I cannot use it this way: MyClass f = std::sqrt

Overriding std functions

泪湿孤枕 提交于 2019-12-23 09:26:16
问题 I'd like to override the behavior of an std function, say std::time. Is it possible to call std::time and have it routed through my custom function? 回答1: The std namespace is off limits, generally speaking. Adding new functions, overloads, classes or anything else to the std namespace is **undefined behavior*. The only exception is template specializations . You may provide specializations of functions in the std namespace. A function where this is often done is std::swap . 回答2: This sounds

Why can't I std::move std::unique_ptrs between std::sets?

…衆ロ難τιáo~ 提交于 2019-12-23 09:16:12
问题 I really want to move some unique_ptr s from one std::set into another: #include <memory> #include <algorithm> #include <set> int main() { std::set<std::unique_ptr<int>> a; std::set<std::unique_ptr<int>> b; a.insert({0, std::unique_ptr<int>(new int(42))}); std::move(a.begin(), a.end(), std::inserter(b, b.end())); } However, my GCC 4.8.5 on CentOS 7 is distinctly unhappy: [root@localhost ~]# g++ test.cpp -std=c++11 -o test In file included from /usr/include/c++/4.8.2/set:60:0, from test.cpp:2:

Better way to determine length of a std::istream?

好久不见. 提交于 2019-12-23 07:49:45
问题 Is there a better way to determine the length of an std::istream than the following: std::istream* pcStream = GetSomeStream(); pcStream->seekg(0, ios::end); unsigned int uiLength = pcStream->tellg(); It just seems really wasteful to have to seek to the end of the stream and then seek back to the original position, especially if the stream might be to a file on some slow media like a CD or DVD. 回答1: The "best" way is to avoid needing the length :) Not all streams are seekable (For example,

Remove all commas, dots and lowercase the string with single iteration

左心房为你撑大大i 提交于 2019-12-23 07:49:30
问题 In my C++ application I need to remove all dots, commas, exclamation marks and to lower case the string. So far I figured out I can do it with std::erase and std::remove like this: string content = "Some, NiceEeeE text ! right HeRe ."; content.erase(std::remove(content.begin(), content.end(), ','), content.end()); content.erase(std::remove(content.begin(), content.end(), '.'), content.end()); content.erase(std::remove(content.begin(), content.end(), '!'), content.end()); std::transform

Remove all commas, dots and lowercase the string with single iteration

こ雲淡風輕ζ 提交于 2019-12-23 07:48:13
问题 In my C++ application I need to remove all dots, commas, exclamation marks and to lower case the string. So far I figured out I can do it with std::erase and std::remove like this: string content = "Some, NiceEeeE text ! right HeRe ."; content.erase(std::remove(content.begin(), content.end(), ','), content.end()); content.erase(std::remove(content.begin(), content.end(), '.'), content.end()); content.erase(std::remove(content.begin(), content.end(), '!'), content.end()); std::transform

Assigning existing values to smart-ptrs?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 07:30:09
问题 I am just learning about smart pointers, and I am having trouble assigning a pre-existing location of a variable to the standard library's shared pointer. For example, lets say you have an int x, which you do not know the value of. With normal pointers, I just did int* ptr; ptr = &x; I tried both that with shared pointers, and std::tr1::shared_ptr<int> ptr; ptr = std::make_shared<int> (&x) So i'm fairly lost as to how to do it. 回答1: You wouldn't (usually) make a smart pointer point to an

Why is the common type of bool and int8_t an int32_t in C++?

随声附和 提交于 2019-12-23 07:28:37
问题 I'm curious about some of the behavior of the builtin bool type in C++. As I understand it, std::common_type determines the common type using implicit convertibility. I would expect that an expression with bool and another type would cause bool to convert to that type. For instance, I can see that bool + float -> float and bool + double -> double . However, bool + int8_t -> int32_t and bool + int16_t -> int32_t . Why is this the case? 回答1: Short answer: integral promotion . In numerical

Remove reference with const references

巧了我就是萌 提交于 2019-12-23 07:21:06
问题 For a parametric class C, I want to get always the "primitive" type irrespective of pointer, const or reference modifiers. template<typename __T> class C { public: typedef std::some_magic_remove_all<__T>::type T; } int main() { C<some_type>::type a; } For example, for some_type equal to: int& int** int*& int const && int const * const and so on I want a is always of type int . How can I achieve it? 回答1: template<class T> struct remove_all { typedef T type; }; template<class T> struct remove

Remove reference with const references

坚强是说给别人听的谎言 提交于 2019-12-23 07:21:04
问题 For a parametric class C, I want to get always the "primitive" type irrespective of pointer, const or reference modifiers. template<typename __T> class C { public: typedef std::some_magic_remove_all<__T>::type T; } int main() { C<some_type>::type a; } For example, for some_type equal to: int& int** int*& int const && int const * const and so on I want a is always of type int . How can I achieve it? 回答1: template<class T> struct remove_all { typedef T type; }; template<class T> struct remove