std

Retrieving size of datatype from std::type_info

删除回忆录丶 提交于 2019-12-10 12:59:07
问题 In C++03, when you use the operator typeid, a type_info object is returned. Is it possible to retrieve the size of the given type based only on this result, such as returned by the sizeof operator? For example: std::type_info info = typeid(int); int intSize = sizeof(int); int intSize2 = info.getSize(); // doesn't exist! The issue is that we use a third-party multi array class that gives back a type_info, but not the size of the type. 回答1: The best way I can see (I would like to be proven

std::move between std::string and std::vector<unsigned char>

余生长醉 提交于 2019-12-10 12:34:57
问题 I am working with 2 libraries. One takes in and returns std::string s while the other uses std::vector<unsigned char> s. It would be good if I could steal the underlying arrays from std::string and std::vector<unsigned char> and be able to move them into each other without the excessive copying. ATM I use something like: const unsigned char* raw_memory = reinterpret_cast<const unsigned char*>(string_value.c_str()), std::vector<unsigned char>(raw_memory, raw_memory + string_value.size(); And

Simple templated function to convert std::vectors - “illegal use of this type as an expression”

霸气de小男生 提交于 2019-12-10 12:19:28
问题 I wrote a quick method to convert std::vectors from one type to another: template<class A, class B> vector<B> ConvertSTDVector_AToB(vector<A> vector) { vector<B> converted_vector; for(unsigned int i= 0; i< vector.size(); i++) converted_vector.push_back(vector[i]); return converted_vector; } But the compiler errors with "error C2275: 'B' : illegal use of this type as an expression" on the line after the opening bracket. At first I thought somehow 'B' was defined elsewhere, but changing both

Removing using namespace std causes the program to get crap results

家住魔仙堡 提交于 2019-12-10 11:45:39
问题 I have a complicated least-squares fitting program, which I was recently debugging. I was debugging stuff by cout-ing them to the console, and to make it easier, I used using namespace std; and funnily, after weeks of coding, I wanted to remove this because debugging is done, and the surprise was that removing it causes the result to be wrong! I did a complete abstract check in g++ and icpc (intel compiler) where I remove and restore this directive, and when it's removed, the result it wrong.

Error handling. Mapping system error codes to generic

最后都变了- 提交于 2019-12-10 11:24:29
问题 I've found that function default_error_condition doesn't work as expected in my code auto ec = std::system_category().default_error_condition(EACCES); std::cout << ec.value() << std::endl << ec.category().name() << std::endl; Returned ec value has system error category, but it has to be generic , if I got it right from the documentation e.g. cppreference and gcc source code system_error.cc UPD: also found this remark in the standard 19.5.1.5 Error category objects The object’s default_error

std::sort and custom swap function

泪湿孤枕 提交于 2019-12-10 10:47:33
问题 I currently have an array of pair<double, int> which I sort using a simple custom comparator function e.g. // compare by first int sort_index_lcomparator(const pair<double, int>& a, const pair<double, int>& b) { return a.first < b.first; } // then sort simply like pair<double, int> arr[size]; std::sort(arr, arr + size, sort_index_lcomparator); I'm actually interested in the index order and not in the sorted doubles. My problem is that I would like to change away from this structure and have

Why is “using namespace std;” considered bad practice?

可紊 提交于 2019-12-10 10:40:17
问题 I've been told by others that writing using namespace std; in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std; considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? Does it impact performance? 回答1: This is not related to performance at all. But consider this: you are using two libraries called Foo and Bar: using namespace foo; using

std::string as a key in std::map using a compare operator

我的梦境 提交于 2019-12-10 10:25:47
问题 I'm trying to use a std::string as a key in a std::map however, i'm unable to find() correctly. My code is somewhat complicated and large so this is a small program that demonstrates the problem I'm having. If someone could tell me why this doesn't work, i'd be very grateful. Thanks. #include <stdio.h> #include <string> #include <map> struct comparer { public: bool operator()(const std::string x, const std::string y) { return x.compare(y)==0; } }; int main(int argc, char *argv[]) { std::map

Limiting fps with std::chrono

时光怂恿深爱的人放手 提交于 2019-12-10 10:24:07
问题 std::chrono::system_clock::time_point m_BeginFrame = std::chrono::system_clock::now(); std::chrono::system_clock::time_point m_EndFrame = std::chrono::system_clock::now(); std::chrono::nanoseconds m_WorkTime = std::chrono::nanoseconds::zero(); std::chrono::nanoseconds m_WaitTime = std::chrono::nanoseconds::zero(); auto invFpsLimit = std::chrono::nanoseconds(1e9 / fpsLimit()); // main loop while (!glfwWindowShouldClose(m_pScreen->glfwWindow())) { m_WaitTime = m_BeginFrame - m_EndFrame; m

Is it possible to bind() *this to class member function to make a callback to C API

痴心易碎 提交于 2019-12-10 09:28:34
问题 Is there a way to use boost or std bind() so I could use a result as a callback in C API? Here's sample code I use: #include <boost/function.hpp> #include <boost/bind/bind.hpp> typedef void (*CallbackType)(); void CStyleFunction(CallbackType functionPointer) { functionPointer(); } class Class_w_callback { public: Class_w_callback() { //This would not work CStyleFunction(boost::bind(&Class_w_callback::Callback, this)); } void Callback(){std::cout<<"I got here!\n";}; }; Thanks! 回答1: No, there