Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-type variadic template parameter [duplicate]

I want to make class with variadic wchar* value arguments. Consider the following example.

template<const wchar_t* ...properties>
class my_iterator{
public:
     std::tuple<std::wstring...> get(); // quantity of wstrings depend on quantity of template parameters
};

I want to use that like the following

my_iterator<L"hello", L"world"> inst(object_collection);
while(inst.next()){
    auto x = inst.get();
}

But I receive compile error, when I instantiate the class.

error C2762: 'my_iterator': invalid expression as a template argument for 'properties'

What's wrong and what to do?

like image 290
Alexey Subbota Avatar asked Aug 03 '26 07:08

Alexey Subbota


1 Answers

What's wrong is [temp.arg.nontype] §2.3. String literals cannot (currently) be used as template arguments. What you could do, for example, is declare named array objects and use those as arguments:

template<const wchar_t* ...properties>
class my_iterator {};


int main()
{
    static constexpr const wchar_t a[] = L"hello";
    static constexpr const wchar_t b[] = L"world";

    my_iterator<a, b> inst;
}

working example here

like image 118
Michael Kenzel Avatar answered Aug 06 '26 01:08

Michael Kenzel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!