Home > Software engineering >  Deduce type from `static constexpr` to `using`
Deduce type from `static constexpr` to `using`

Time:02-11

is it possible to "deduce" the type of a static constexpr to a using?

https://compiler-explorer.com/z/hKzqhv7Pa

#include <chrono>

// Bar.h
using Bar = std::chrono::milliseconds;               
static constexpr std::chrono::milliseconds BAR{100};

// Foo.h
struct Foo {
  // Is there a way to get ride of the `using Bar` 
  // and "deduce" the type of `BAR` in a  (elegant) 
  // way to the `using Type`?
  using Type = Bar // decltype(BAR); // does not compile
  static constexpr auto FOO = BAR;
};

int main() {
  Foo::Type x{0};
  x = Foo::FOO; // candidate function not viable: 'this' argument has type 'Foo::Type' (aka 'const duration<long, ratio<1, 1000> >'), but method is not marked const
  return 0;
}

Thanks for the help

Zlatan

CodePudding user response:

decltype(BAR) does work. It just returnrs a const type, so the assignment in main fails. Use std::remove_const_t<decltype(BAR)>.

  • Related