J
John paul coder
Guest
Hello All,
In the template function we are using "explicit" keyword. I wanted to know why to use "explicit" keyword with template.
Below is the code snippet:
RegistryValue.h file
enum RegistryValueKind
{
pkEmpty = 0,
pkBool = 1,
pkArray = 2
};
class RegistryValue
{
private:
RegistryValueKind mKind;
public:
typedef std::vector<RegistryValue*> ArrayVal;
public:
bool SetValue(const RegistryValue& value)
{
mValue = value;
return true;
}
template <typename T>
explicit RegistryValue(const std::vector<T> & collection) : mKind(pkEmpty)
{
int count = 0;
typename std::vector<T>::const_iterator i;
for (i = collection.begin(); i != collection.end(); i++)
{
(*this)[count++] = *i;
}
}
Because, from the other .cpp file when i am trying to insert a vector into the SetValue function I am getting the below error:
no suitable user-defined conversion from "std::vector<std::wstring,std::allocator<std::wstring>>" to "const RegistryValue" exists.
I resolved this error by removing the explicit keyword. Is it the correct way?
Thanks in Advance,
Continue reading...
In the template function we are using "explicit" keyword. I wanted to know why to use "explicit" keyword with template.
Below is the code snippet:
RegistryValue.h file
enum RegistryValueKind
{
pkEmpty = 0,
pkBool = 1,
pkArray = 2
};
class RegistryValue
{
private:
RegistryValueKind mKind;
public:
typedef std::vector<RegistryValue*> ArrayVal;
public:
bool SetValue(const RegistryValue& value)
{
mValue = value;
return true;
}
template <typename T>
explicit RegistryValue(const std::vector<T> & collection) : mKind(pkEmpty)
{
int count = 0;
typename std::vector<T>::const_iterator i;
for (i = collection.begin(); i != collection.end(); i++)
{
(*this)[count++] = *i;
}
}
Because, from the other .cpp file when i am trying to insert a vector into the SetValue function I am getting the below error:
no suitable user-defined conversion from "std::vector<std::wstring,std::allocator<std::wstring>>" to "const RegistryValue" exists.
I resolved this error by removing the explicit keyword. Is it the correct way?
Thanks in Advance,
Continue reading...