temporary

Generate the Cartesian Product of 2 vector<string>s In-Place?

末鹿安然 提交于 2019-12-02 02:50:30
If I want to get the Cartesian Product of these two vector<string> s: vector<string> final{"a","b","c"}; vector<string> temp{"1","2"}; But I want to put the result in final , such that final would contain: a1 a2 b1 b2 c1 c2 I'd like to do this without creating a temporary array. Is it possible to do this? If it matters, the order of final is not important. You may try the following approach #include <iostream> #include <vector> #include <string> int main() { std::vector<std::string> final{ "a", "b", "c" }; std::vector<std::string> temp{ "1", "2" }; auto n = final.size(); final.resize( final

Is it a bug that Microsoft VS C++ compiler can Initialize a reference from a temporary object [duplicate]

本秂侑毒 提交于 2019-12-01 08:25:15
Possible Duplicate: Binding temporary to a lvalue reference With VS2008 C++ compiler, the codes are compiled without compile error. class A{}; int main(){ A& a_ref = A(); return 0; } I believe the C++ standard, both C++2003 and coming C++0x, disallow it. And I also get a compile time error with gcc compiler. So what I want to know is, is this a known bug for VS compiler to allow initializing reference from a temporary object. Or is it a feature extension of VS compiler? If yes, what's life cycle of the temporary object? It is the extension.This link explains it. What if we take out the const

can MySQL temporary variables be used in WHERE clause?

可紊 提交于 2019-12-01 06:24:05
In MySQL, can temporary variables be used in the WHERE clause? For example, in the following query: SELECT `id`, @var := `id` * 2 FROM `user` @var is successfully set to twice the value of `id` However, if I try to filter the result set to only include results where @var is less than 10: SELECT `id`, @var := `id` * 2 FROM `user` WHERE @var < 10 then I get no results. How can I filter the results based on the value of @var? You need to assign an alias, and test it in the HAVING clause: SELECT id, @var := id * 2 AS id_times_2 FROM user HAVING id_times_2 < 10 Note that if you're just using the

C/C++ Warning: address of temporary with BDADDR_ANY Bluetooth library

淺唱寂寞╮ 提交于 2019-12-01 05:27:27
问题 I'm having some problems with g++ and the compiling process for a C/C++ program which use Bluetooth libraries under Ubuntu. If i use gcc, it works fine with no warning; on the contrary, if i use g++ i get this warning: warning: taking address of temporary even if the program compiles fine and it works. The involved lines reporting the error are: bdaddr_t *inquiry(){ // do some stuff.. bacpy(&result[mote++], BDADDR_ANY); return result; } //... void zeemote(){ while (bacmp(bdaddr, BDADDR_ANY)){

Is it a bug that Microsoft VS C++ compiler can Initialize a reference from a temporary object [duplicate]

断了今生、忘了曾经 提交于 2019-12-01 05:09:01
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Binding temporary to a lvalue reference With VS2008 C++ compiler, the codes are compiled without compile error. class A{}; int main(){ A& a_ref = A(); return 0; } I believe the C++ standard, both C++2003 and coming C++0x, disallow it. And I also get a compile time error with gcc compiler. So what I want to know is, is this a known bug for VS compiler to allow initializing reference from a temporary object. Or is

can MySQL temporary variables be used in WHERE clause?

放肆的年华 提交于 2019-12-01 04:21:51
问题 In MySQL, can temporary variables be used in the WHERE clause? For example, in the following query: SELECT `id`, @var := `id` * 2 FROM `user` @var is successfully set to twice the value of `id` However, if I try to filter the result set to only include results where @var is less than 10: SELECT `id`, @var := `id` * 2 FROM `user` WHERE @var < 10 then I get no results. How can I filter the results based on the value of @var? 回答1: You need to assign an alias, and test it in the HAVING clause:

How would auto&& extend the life-time of the temporary object?

☆樱花仙子☆ 提交于 2019-12-01 03:23:54
The code below illustrated my concern: #include <iostream> struct O { ~O() { std::cout << "~O()\n"; } }; struct wrapper { O const& val; ~wrapper() { std::cout << "~wrapper()\n"; } }; struct wrapperEx // with explicit ctor { O const& val; explicit wrapperEx(O const& val) : val(val) {} ~wrapperEx() { std::cout << "~wrapperEx()\n"; } }; template<class T> T&& f(T&& t) { return std::forward<T>(t); } int main() { std::cout << "case 1-----------\n"; { auto&& a = wrapper{O()}; std::cout << "end-scope\n"; } std::cout << "case 2-----------\n"; { auto a = wrapper{O()}; std::cout << "end-scope\n"; } std:

How would auto&& extend the life-time of the temporary object?

回眸只為那壹抹淺笑 提交于 2019-12-01 00:19:56
问题 The code below illustrated my concern: #include <iostream> struct O { ~O() { std::cout << "~O()\n"; } }; struct wrapper { O const& val; ~wrapper() { std::cout << "~wrapper()\n"; } }; struct wrapperEx // with explicit ctor { O const& val; explicit wrapperEx(O const& val) : val(val) {} ~wrapperEx() { std::cout << "~wrapperEx()\n"; } }; template<class T> T&& f(T&& t) { return std::forward<T>(t); } int main() { std::cout << "case 1-----------\n"; { auto&& a = wrapper{O()}; std::cout << "end-scope

Are temporary objects in C++ const indeed?

自闭症网瘾萝莉.ら 提交于 2019-11-30 22:54:23
I always believed that temporary objects in C++ are automatically considered as const by the compiler. But recently I experienced that the following example of code: function_returning_object().some_non_const_method(); is valid for C++ compiler. And it makes me wonder - are temporary objects in C++ const indeed? If yes, then why the code above is considered correct by the compiler? No, they're not. Not unless you declare the return type as const. It depends. int f(); const int g(); class C { }; C x(); const C y(); In the case of both f() and g() , the returned value is not const because there

Address of a temporary in Go?

守給你的承諾、 提交于 2019-11-30 11:46:52
问题 What's the cleanest way to handle a case such as this: func a() string { /* doesn't matter */ } b *string = &a() This generates the error: cannot take the address of a() My understanding is that Go automatically promotes a local variable to the heap if its address is taken. Here it's clear that the address of the return value is to be taken. What's an idiomatic way to handle this? 回答1: The address operator returns a pointer to something having a "home", e.g. a variable. The value of the