Ofek Gila
Ofek Gila

Reputation: 703

error: missing template arguments before for constexpr member variables

I have looked at some of the similar questions, and haven't found any solution that works for my use case. I have a constexpr variable that I want to template with default arguments, and I can't seem to get it to work:

// Test.hpp
class Test
{
public:
    template <bool val = false>
    constexpr static bool MY_BOOL = val;
};

// Test.cpp
#include "Test.hpp"
#include <iostream>

int main()
{
    std::cout << Test::MY_BOOL << "\n";
    return 0;
}

g++ compiler error:

Test.cpp: In function ‘int main()’:
Test.cpp:6:29: error: missing template arguments before ‘<<’ token
  std::cout << Test::MY_BOOL << "\n";

Please let me know what I'm doing wrong / if this can be fixed. Thanks!

Upvotes: 1

Views: 932

Answers (1)

aschepler
aschepler

Reputation: 72271

The template argument list following a template name can be omitted only for a function template. For a variable template, class template, or alias template, even if you don't want to provide any explicit arguments, you need at least the empty list <>.

To use your default template argument of false:

std::cout << Test::MY_BOOL<> << "\n";

Upvotes: 2

Related Questions