0

How I can initialize a atomic variable into struct, boost::atomic MAX

I tried:

#include <boost/atomic.hpp>

struct mem {
    // error: conversion from ‘int’ to non-scalar type ‘boost::atomics::atomic<int>’ requested
    boost::atomic<int> MAX = 100;

    // error: expected identifier before numeric constant
    boost::atomic<int> MAX(100);

    // error: ‘boost::atomics::atomic<T>::atomic(const boost::atomics::atomic<T>&) [with T = int]’ is private
    boost::atomic<int> MAX = (boost::atomic<int>) 100;

    // warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
    boost::atomic<int> MAX{100};
}

Note: I can't use c++11 or c++14.

2 Answers2

0

This form should work

boost::atomic<int> MAX(100);

If it doesn't, that probably means MAX token is replaced by the preprocessor. Try to eliminate headers, use a different variable name etc.

See also

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
0

If you need to initialize a struct/class member in C++03 then you have to write a constructor.

struct mem {
    boost::atomic<int> MAX;

    mem() : MAX(100)
    {
    }
};

PS: sehe is correct to warn you that MAX can be a macro on some systems, you should be careful about upper-case names.

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27