Using C++11 (and later) standard library

Hi community,

I was trying to compile a minimal example of a C++ program using a function from the standard library, that is available only in C++11 and newer, namely std::log2 from the cmath header. However, I am getting an error ‘log2’ is not a member of ‘std’; did you mean ‘log’? when compiling.

Question: Why is std::log2 not available after #include <cmath>? Am I missing something (build options, …)?

Some related info:

  • project for reference: testCppStd.zip (37.0 KB) (AS6.3, AR 6.4.2)
  • program appears to be compiled with C++ >= 11, at least static_assert(__cplusplus >= 201103L, “This code requires C++11 or later”); does not cause a compile error
  • adding -std=c++11 to the additional build options did not change the situation

here is the source of the minimal C++ example:

#include <bur/plctypes.h>

#ifdef _DEFAULT_INCLUDES
    #include <AsDefault.h>
#endif

static_assert(__cplusplus >= 201103L, "This code requires C++11 or later");

#include <cmath>

// amount of memory to be allocated for heap storage must be specified for every ANSI C++ program with the bur_heap_size variable
unsigned long bur_heap_size = 0xFFFF; 

void _CYCLIC ProgramCyclic(void)
{
    double a = std::log2(4.0);
    a = a + a;  // silence "unused variable"
}

Best regards

Lukas

Hello,

Unfortunately, I can’t say.

But you can implement it by yourself:

#define log2(x) (log (x) / _M_LN2)

Thanks Christoph, this is a workaround for the specific case, sure.

However, log2 was merely chosen as a more or less arbitrary example for any “new” function in std from <cmath>. Others, like std::trunc etc. show the same behavior. So code relying on a recent std-lib will fail, that is my point.

To check if this is a more general behavior, I tried to use std::unordered_map from the <unordered_map> header which compiled without problems. So there are C++11 std-lib features that work as expected.

it looks like ‘log2’ can be used without std:: namespace :slight_smile:

and this is because cmath first includes math.h and then defines the std namespace.

But according to C++ standard std::log2 should work as well.

@Lukas_Kimme : I will clarify this with B&R headquarter and come back to you.

1 Like

@christoph.hilchenbac, I guess there are no updates yet?

unfortunately not yet. It is still being processed by R&D.

1 Like

Hi @christoph.hilchenbac , I marked your reply as a solution, please update us with findings from R&D.

1 Like

The issue is currently being investigated under QCAS-4148.