Avoiding self assignment in std::shuffle

The libstdc++ Debug Mode assertion is based on this rule in the standard, from [res.on.arguments] If a function argument binds to an rvalue reference parameter, the implementation may assume that this parameter is a unique reference to this argument. i.e. the implementation can assume that the object bound to the parameter of T::operator=(T&&) does not … Read more

How do I install g++ on MacOS X?

That’s the compiler that comes with Apple’s XCode tools package. They’ve hacked on it a little, but basically it’s just g++. You can download XCode for free (well, mostly, you do have to sign up to become an ADC member, but that’s free too) here: http://developer.apple.com/technology/xcode.html Edit 2013-01-25: This answer was correct in 2010. It … Read more

C++ template function compiles in header but not implementation

The problem you’re having is that the compiler doesn’t know which versions of your template to instantiate. When you move the implementation of your function to x.cpp it is in a different translation unit from main.cpp, and main.cpp can’t link to a particular instantiation because it doesn’t exist in that context. This is a well-known … Read more

Linking using g++ fails searching for -lstdc++

Posting for future reference, a solution I found was to install g++-multilib. I had the same incompatible problem relating to -lstdc++ on g++ version 4.6.1 On further probing: g++-multilib is a dummy package which installed g++4.6-multilib which in turn installed the appropriate libstdc++.so under the /usr/lib/gcc/x86_64-linux-gnu/4.6/32 folder.

Difference between regex_match and regex_search?

Assuming that C++ and Boost Regex have a similar structure and functionality, the difference between regex_match and regex_search is explained here: The regex_match() algorithm will only report success if the regex matches the whole input, from beginning to end. If the regex matches only a part of the input, regex_match() will return false. If you … Read more

Linking error: undefined reference to `vtable for XXX`

You never provided an implementation for virtual ~Interval(); and several other functions. You must provide an implementation for all non-pure virtual functions you declare. In particular, G++ emits the vtable along with the implementation of the first-declared non-inline function in a class. Omitting its implementation means you won’t have a vtable, and thus won’t be … Read more

What does CC?= in a Makefile mean?

From http://www.gnu.org/software/make/manual/make.html: There is another assignment operator for variables, `?=’. This is called a conditional variable assignment operator, because it only has an effect if the variable is not yet defined. This statement: FOO ?= bar is exactly equivalent to this (see The origin Function): ifeq ($(origin FOO), undefined) FOO = bar endif Probably CC … Read more

__attribute__((weak)) and static libraries

To explain what’s going on here, let’s talk first about your original source files, with a.h (1): void foo() __attribute__((weak)); and: a.c (1): #include “a.h” #include <stdio.h> void foo() { printf(“%s\n”, __FILE__); } The mixture of .c and .cpp files in your sample code is irrelevant to the issues, and all the code is C, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)