c++98

Convert a std::vector of a boost::variant type

你说的曾经没有我的故事 提交于 2019-12-06 07:04:47
问题 How might I implement the function below to convert from vector of Value to a Container ? I wish to assert if not all the members of values are of the same type, i.e. if the vector contains a mix of strings and ints. This is because the function's return value is either a std::vector<int> or a std::vector<std::string> . typedef boost::variant<int, std::string> Value; typedef boost::variant<std::vector<int>, std::vector<std::string> > Container; Container valuesToContainer(const std::vector

Legal legacy code using pointers suddenly becomes UB

我们两清 提交于 2019-12-05 20:07:40
Let's say we have this legacy code from C++98: bool expensiveCheck(); struct Foo; bool someFunc() { Foo *ptr = 0; if( expensiveCheck() ) ptr = new Foo; // doing something irrelevant here ... if( ptr ) { // using foo } delete ptr; return ptr; // here we have UB(Undefined Behavior) in C++11 } So basically pointer here is used to keep dynamically allocated data and use it as a flag at the same time. For me it is readable code and I believe it is legal C++98 code. Now according to this questions: Pointers in c++ after delete What happens to the pointer itself after delete? this code has UB in C+

Where does the C++98 standard specify when a call to a static member is dependent within a template?

若如初见. 提交于 2019-12-05 11:48:16
Compiling with Clang 3.0 -std=c++98, the following code is accepted: template<int> struct I { typedef int Type; }; template<class> struct S { static int f(int); //static int f(int*); // implicitly instantiates I<sizeof(int)> typedef I<sizeof(f(0))>::Type Type; }; S<int>::Type s; Uncommenting the overload of 'f' causes Clang to report an error "missing 'typename' prior to dependent type name". G++ 4.8 reports the same error with or without the overload. msvc10 does not give any errors with or without the overload. Where does the standard say whether or not 'f' is dependent and 'typename' is

In GCC, Clang and MSVC is there any way to conform against C++98 and not C++03?

霸气de小男生 提交于 2019-12-05 07:43:33
The meta question proposes that the c++98 and c++03 tags should be made synonyms. The question asker followed it up with Is value initialization part of the C++98 standard? If not, why was it added in the C++03 standard? , an excellent question which sheds light on the addition of value initialization to C++03. Consider this question to be a follow-up to the latter. The OP asserts that modern compilers do not bother distinguishing between C++98 and C++03. This was surprising to me, as it turns out to be the case for three modern compilers. Although this question could boil down to "RTFM", my

template magic for wrapping C callbacks that take void* parameters?

自闭症网瘾萝莉.ら 提交于 2019-12-05 00:17:48
Say I'm using a C API that lets you register callbacks that take a void* closure: void register_callback(void (*func)(void*), void *closure); In C++ it's nice to have stronger types than void* so I want to create a wrapper that lets me register strongly-typed C++ callbacks instead: template <typename T, void F(T*)> void CallbackWrapper(void *p) { return F(static_cast<T*>(p)); } void MyCallback(int* param) {} void f(void *closure) { register_callback(CallbackWrapper<int, MyCallback>, closure); } This works alright. One nice property of this solution is that it can inline my callback into the

Why is std::list bigger on c++11?

最后都变了- 提交于 2019-12-04 15:17:25
问题 with this code: #include <iostream> #include <list> int main() { std::cout << sizeof(std::list<void*>) << std::endl; }; I managed to notice that on GCC 4.7 the size of std::list<void*> on C++98 is 16 bytes, and its size on C++11 is 24 bytes. I was wondering what changed on std::list that made it bigger. 回答1: C++11 requires list::size() to execute in constant time. GCC made this possible by adding the size as a data member. GCC did not do so for C++98 mode, because that would break binary

std::copy/memcpy/memmove optimizations

假装没事ソ 提交于 2019-12-04 04:10:24
I looked into the GCC STL (4.6.1) and saw that std::copy() uses an optimized version in case the builtin __is_trivial() evaluates to true . Since the std::copy() and std::reverse_copy() templates are very useful for copying elements in arrays, I would like to use them. However, I have some types (which are results of template instantiations) that are structs that contain some trivial values, no pointers and have no copy constructor or assignment operator. Is G++ smart enough to figure out that my type in fact is trivial? Is there any way in C++98 to make sure an STL implementation knows that

Using SFINAE to detect a member function [duplicate]

混江龙づ霸主 提交于 2019-12-04 03:29:21
This question already has answers here : Closed 6 years ago . Is it possible to write a template to check for a function's existence? (25 answers) In C++11, to find out whether a class has a member function size , you could define the following test helper: template <typename T> struct has_size_fn { typedef char (& yes)[1]; typedef char (& no)[2]; template <typename C> static yes check(decltype(&C::size)); template <typename> static no check(...); static bool const value = sizeof(check<T>(0)) == sizeof(yes); }; Is there a similar trick for doing this in C++98 without relying on compiler

int a=int(); what happens in C++98?

孤街浪徒 提交于 2019-12-04 03:16:10
Please read the question entirely before you think to mark it as duplicate. The statement like int i=int(); most programmers will say that there is value initialization here & i will be value initialized. (0 as output). But it also prints 0 as output on C++98 compiler. Following program that I tested on C++98 implementation and gives me 0 as output. #include <iostream> int main() { int i=int(); std::cout<<i; } Don't say that i is value initialized in above C++98 program ,because value initialization introduced in C++03. So How i is initialized here? Is it really constructor call? int() looks

port string interpolation from c++14 to c++98

北城以北 提交于 2019-12-04 02:38:51
问题 I'm trying to port this answer: Replace N formulas to one (string interpolation) to a standard c++98 implementation. C++14 version: #include <algorithm> #include <iostream> #include <iterator> #include <map> #include <string> using namespace std; int main() { map<string, string> interpolate = { { "F"s, "a && b && c"s }, { "H"s, "p ^ 2 + w"s }, { "K"s, "H > 10 || e < 5"s }, { "J"s, "F && !K"s } }; for(const auto& i : interpolate) for_each(begin(interpolate), end(interpolate), [&](auto& it){