I am using CRTP to implement something but am stuck on an error in XCode 4.5.2. The following code is a simplified version that still replicates the error. It occurs on the line where the method Api::Enable is defined and has something to do with the fact that there are no parameters when Api::Enable calls this->T::Enable
enum Enum
{
FOO,
BAR,
BAZ,
};
template <typename T>
class Api
{
public:
template <Enum E, bool On> void Enable() {static_cast<T *>(this)->Enable<E, On>();}
};
class ApiImpl : public Api<ApiImpl>
{
public:
template <Enum E, bool On> void Enable() {}
};
int main(int argc, const char * argv[])
{
ApiImpl clsApi;
clsApi.Enable<FOO, true>();
return 0;
}
Here is a screenshot of the error in Xcode: https://i.stack.imgur.com/YUHtt.png. I get the same error whether I use "Apple LLVM compiler 4.1" or "LLVM GCC 4.2". MSVC Express 2010 compiles without error.
Note that adding a function parameter causes the error to disappear. The following compiles fine:
enum Enum
{
FOO,
BAR,
BAZ,
};
template <typename T>
class Api
{
public:
template <Enum E , bool On> void Enable(unsigned int X) {static_cast<T *>(this)->Enable<E, On>(X);}
};
class ApiImpl : public Api<ApiImpl>
{
public:
template <Enum E, bool On> void Enable(unsigned int) {}
};
int main(int argc, const char * argv[])
{
ApiImpl clsApi;
clsApi.Enable<FOO, true>(0);
return 0;
}
You should use
templatekeyword to resolve dependent template names:C++11,
[temp.names]/4:If
Enable()is for exampletemplate <typename T> void Enable(){}, then clang shows an error:error: use 'template' keyword to treat 'Enable' as a dependent template name.I don't know why it produces that irrelevant message in your case (when template arguments are not types). I think this can be posted as bug report. (I tested on clang 3.6 - the same).
Also, gcc 4.8 and 4.9 don't produce any errors on this code which I beleive is also incorrect.