Bug with literal operators when "using namespace juce" is disabled

i’m disabling using namespace juce in a project (i’m using xcode + juce off of develop). i’ve noticed that in some places literal operators (_px and _fr) stopped working. xcode gives me:

No matching literal operator for call to 'operator""_fr' with argument of type 'unsigned long long' or 'const char *', and no matching literal operator template

example 1

i had some standard code that looks like this:

void FooComponent::resized() {
  Grid grid;
  using Track = Grid::TrackInfo;
  grid.templateRows = {
      Track(1_fr),
  };

this obviously need to be fixed. i tried this:

void FooComponent::resized() {
  juce::Grid grid;
  using Track = juce::Grid::TrackInfo;
  grid.templateRows = {
      Track(1_fr),
  };

and it didn’t work. the only way i could get this working is to locally pull the juce namespace back in :confused: :

void FooComponent::resized() {
using namespace juce;
Grid grid;
using Track = Grid::TrackInfo;
 grid.templateRows = {
     Track(1_fr),
 };

example 2

// in a header file
class CustomLookAndFeel : public LookAndFeel_V4 {
public:
  static constexpr const Grid::Px rowGap{5.0f};
}

this code also stopped working. this didn’t work:

class CustomLookAndFee :  public juce::LookAndFeel_V4l {
public:
  static constexpr const juce::Grid::Px rowGap{5.0f};
}

in the end i fixed this one by initializing the variable in the .cpp file, ie:

// CustomLookAndFeel.h
class CustomLookAndFeel : public juce::LookAndFeel_V4 {
  static const juce::Grid::Px rowGap;
}
// CustomLookAndFeel.cpp
const juce::Grid::Px CustomLookAndFeel::rowGap = 5_px;

any ideas here? is this a juce bug or user error?

This isn’t a JUCE bug, it’s just that the literal operators need to be made visible in the current scope when they are used. To avoid importing the juce namespace in its entirity, you can include just the operators that you care about, like so:

using juce::operator""_px;
using juce::operator""_fr;

It looks like only the Px constructors taking long double and unsigned long long arguments are constexpr. I think this should work:

class CustomLookAndFeel : ...
{
  static constexpr juce::Grid::Px rowGap{5.0L};
};

thanks! that seems pretty obscure - should there be a note in the docs for this? projucer definitely recommends not using namespace juce for new projects but there’s a lot of example code that uses these.