Moving a member function from base class to derived class breaks the program for no obvious reason

This is why you should never using namespace std;

bool foo(T n, string s)
{
    return bitset < 32                  
           && 63 > (~n & 255) == oldmode 
           && regex_match(s, sig_regex);
}

That bitset isn’t what you think it is. Because B<T> is a dependent base class, members are hidden from unqualified lookup. So to access bitset, you need to access it through this1, or explicitly qualify it (see here for more details):

(this->bitset)
B<T>::bitset

Because bitset doesn’t name B<T>::bitset in the derived case, what could it mean? Well, because you wrote using namespace std;, it’s actually std::bitset, and the rest of your expression just so happens to be valid. Here’s what happens:

bool foo(T n, string s)
{
    return std::bitset<32 && 63>(~n & 255) == oldmode 
           && regex_match(s, sig_regex);
}

The 32 && 63 evaluates to true, which is promoted to 1u for the std::bitset template argument. This std::bitset is initialized with ~n & 255, and is checked for equality with oldmode. This last step is valid because std::bitset has a non-explicit constructor which allows a temporary std::bitset<1> to be constructed from oldmode.


1 Note that we need to parenthesise this->bitset in this case due to some pretty subtle parsing disambiguity rules. See Template dependent base member is not resolved properly for details.

Leave a Comment

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