Non type template parameter

user3689963

Consider this:

#include <set>

template <typename T, T val>
class sample {
public:
    // Return val for new elements.
    T& at(unsigned i) {
       auto o = _set.insert(val);
       return *o.first;
    }

private:
    std::set<T> _set;
};

class s {
public:
    constexpr s() = default;
};


int main() {
    constexpr s val;
    sample<s, val> o2;
    return 0;
}

gcc -std=c++11 doesn't compile.

non-type.cc: In function ‘int main()’:
non-type.cc:24:18: error: ‘class s’ is not a valid type for a template non-type parameter
     sample<s, val> o2;

As mentioned in the comment, I want new elements of 'set' are initialized to 'val'. As val is constant, it looks reasonable expectation!

Can someone please tell me how I can achieve it?

ShadowMitia

It looks like what you want to do is possible, but requires C++20.

From the docs

Until C++20 a "non-type parameter" needs to be one of

- std::nullptr_t (since C++11);
- an integral type;
- a pointer type (to object or to function);
- a pointer to member type (to member object or to member function);
- an enumeration type.

Having a non-type parameter of a custom type seems to not be available at the moment unless you have access to a compiler that already has that feature.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

template non-type template parameter

Alias Template with non-type template parameter

Specialize template template parameter with a non-type template parameter

Non-type template parameter dependent on a default template type parameter

Function template taking a template non-type template parameter

Using non-type template template parameter in template specialisation

non type template parameter pack expansion

pointer non-type template parameter

Non-type variadic template parameter

Why is it called "non-type" template parameter?

Template function dependent on non-type parameter

Template non-type parameter deduction

Template non-type parameter with different types

Integer sequence as a non-type template parameter

Template template parameter with mixed type and non-type variadic parameters

Syntax for an instance of a class template as a non-type template parameter

static_assert in function template with non-type template parameter

C++ template template non-type parameter

template parameter deduction from templated non-type template argument

Have a template parameter that can be pointer type or non-pointer type

Class type non-type template parameter initialization does not compile

T is not a valid type for non-type template parameter

Partially specializing on non-type template parameter of the wrong type

Can a non-type template parameter be of type "void*"?

Determine type from non-type template parameter

C++ Template specialization by type of non-type parameter

Non-type template parameter type changes randomly

a non-type template parameter cannot have type

Is it possible to have a "generic" template parameter in C++, that can be either a non-type template parameter or a type?