5
#include <atomic>
std::atomic<int> outside(1);
class A{
  std::atomic<int> inside(1);  // <--- why not allowed ?
};

error:

prog.cpp:4:25: error: expected identifier before numeric constant
prog.cpp:4:25: error: expected ',' or '...' before numeric constant

In VS11

C2059: syntax error : 'constant'
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Try inside = std::atomic(0); –  Sep 09 '12 at 12:31
  • I think there is a historic problem with `()` inside the class. Have you tried `=` instead of braces ? – iammilind Sep 09 '12 at 12:32
  • @iammilind guess what. It works outside but not inside..... –  Sep 09 '12 at 12:34
  • [related](http://stackoverflow.com/questions/6482566/has-in-class-member-initialization-feature-made-into-c11) and [other one](http://stackoverflow.com/questions/9723164/c11-initializer-lists-uniform-initialization). – iammilind Sep 09 '12 at 12:39

1 Answers1

8

In-class initializers do not support the (e) syntax of initialization because the committee members that designed it worried about potential ambiguities (For example, the well-known T t(X()); declaration would be ambiguous and does not specify an initialization but declares a function with an unnamed parameter).

You can say

class A{
    std::atomic<int> inside{1};
};

Alternatively a default value can be passed in the constructor

class A {
  A():inside(1) {}
  std::atomic<int> inside;
};
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 2
    although correct ATM both gcc and VC11 doesn't support `{1}` :(. +1 –  Sep 09 '12 at 12:33
  • 1
    Non-static data member initializers ([N2756](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2008/n2756.htm)) are supported by Clang 3.0 and upwards. – Matthieu M. Sep 09 '12 at 12:36
  • @MatthieuM. I'm on windows :(. My favorite IDE is VS and i like clang but it doesnt support windows –  Sep 09 '12 at 12:41
  • @acidzombie24: I can understand the appeal of VS (especially its debugger). As for Windows support in clang, it's moving along but yes, unfortunately, it's still lagging behind. Personally (having a Windows computer at home too), I use codeblocks for edition on a shared folder... and I have a Ubuntu VM for compiling/running my programs :) – Matthieu M. Sep 09 '12 at 12:44
  • @MatthieuM I'm assuming CB is running in the VM and all the dev work is done there? I test via a poorly written but working `makeIt.sh` in linux and ran once in a while. Everything else is done on windows. I also have a `gccbuild.bat` –  Sep 09 '12 at 12:59
  • @acidzombie24 [GCC does support it](http://liveworkspace.org/code/0adafb37b55d228ca5a37d3f2efe6123). – Luc Danton Sep 09 '12 at 13:13
  • @acidzombie24: Actually no, I am editing on Windows and using VirtualBox ability to share folder between host OS and guest OS to access the files from within the Ubuntu VM to compile them :) – Matthieu M. Sep 09 '12 at 13:32
  • @LucDanton: darnnnnnn must upgrade :D. Now if only the brand new released less then a month ago VS supported it, i'd be all set :( –  Sep 09 '12 at 13:37
  • @MatthieuM. nice, how do you send the build command from cb windoze and get the errors/outputs? –  Sep 09 '12 at 13:39
  • 1
    This is C++11 syntax, and it hasn't made it into many compilers yet. – Pete Becker Sep 09 '12 at 16:34