I have MyClass
which is a template class. I wanted to provide an initialize r list constructor so that I can conveniently write:
MyClass<int> Arr0{ 1, 2, 3, 4, 5, 8 };
On the other hand, I do not want to have duplicates in this list as this class meant to have only unique user inputs. I have seen many ways to check the duplicates in the array and I came up with the has_duplicates()
following function.
I tried to combine the idea of checking, whether the std::initializer_list<T>
ed temporary elements(or array) contains any duplicate elements in the member initializer list itself; if it contains static_assert()
the template instantiation and thereby no object of this class will be constructed.
Following is the minimal example of my code.
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <iterator>
#include <initializer_list>
template <typename Iterator> // function to check duplicates(which works fine)
constexpr bool has_duplicates(Iterator start, Iterator end)
{
if (start == end) return false;
using Type = typename std::remove_reference_t<decltype(*end)>;
std::map<Type, std::size_t> countMap;
for (; start != end; ++start)
{
countMap[*start]++;
if (countMap[*start] >= 2) return true;
}
return false;
}
template <typename T> class MyClass
{
private:
std::vector<T> m_vec;
public:
MyClass(std::initializer_list<T> a)
: (has_duplicates(a.begin(), a.end()) //-----> here is the problem
? static_assert(false, " the array has duplicates....")
: m_vec(a)
)
{
std::cout << "Constriction successful....";
}
};
int main()
{
std::vector<int> test{ 1, 2, 3, 4, 1 };
std::cout << std::boolalpha
<< has_duplicates(test.begin(), test.end()) << std::endl; // works
MyClass<int> Arr0{ 1, 2, 3, 4 }; // error
return 0;
}
Upon compiling in MSVC 16.0(C++17 flag), this gives me the error:
error C2059: syntax error: 'static_assert'
note: while compiling class template member function 'MyClass<int>::MyClass(std::initializer_list<_Ty>)'
with
[
_Ty=int
]
note: see reference to function template instantiation 'MyClass<int>::MyClass(std::initializer_list<_Ty>)' being compiled
with
[
_Ty=int
]
note: see reference to class template instantiation 'MyClass<int>' being compiled
error C2143: syntax error: missing ';' before '}'
error C2059: syntax error: ')'
error C2447: '{': missing function header (old-style formal list?)
It says an simple syntax error, but I do not see any as per static_assert.
Can anybody help me find out the error?
What is the correct way to prevent construction of std::initializer_list<T>
constutor arguments, in the above case?