C++ not recognizing 3points operator - c++

I have the following code:
class IConfigItem
{
public:
virtual ~IConfigItem() {};
virtual operator int() const { return 0; }
virtual operator std::string () const { return std::string(); }
};
template< typename T > class ConfigItem : public IConfigItem
{
private:
T m_value;
public:
ConfigItem< T >(const T& valueIn = 0) : m_value(valueIn) {}
operator T() const
{
return m_value;
}
};
template< typename T, typename ...Args > std::unique_ptr< T > makeUnique(Args&& ...args)
{
return std::unique_ptr< T >(new T(std::forward< Args >(args)...));
}
and I am getting the following error:
Error 1 error C2143: syntax error : missing ',' before '...'
I do not get it why. Why doesn't it recognize the ...? What am I doing wrong? I have the same code on Ubuntu, and there it is working well, now I have copied this to VS2012 and it has errors. Please someone help me.

You need Visual Studio 2013 for this see Variadic templates in the Visual Studio c++11 features support page: http://msdn.microsoft.com/en-us/library/hh567368.aspx
The community version of Visual Studio 2013 is available here:
http://visualstudio.com/en-us/downloads

Related

LLVM vs MSVC function disambiguation

A library that I'm porting from Visual Studio to Xcode uses a template chain. My chain works correctly in Visual Studio. However, when I compile it in Xcode using Apple LLVM 6.0, I get the error
References to overloaded function could not be resolved; did you mean to call it?
Update
I've created a standalone example that compiles in Visual Studio but fails to compile in Xcode with the same error that I'm seeing in my library.
Note: I can probably fix my library code by other means. However, I'd like to take this opportunity for someone to teach me something new about disambiguation and the differences between the compilers. Is this pattern non-standard? Did it only work in Visual Studio because of a language extension?
class Test1{};
class Test2{};
struct NullType{};
template< class T, int id>
class SpecialHandler
{
public:
enum { ETypeId = id };
void handlerFunc( float, int ){}
};
template<typename TReturn, class TObj, typename TParam1, typename TParam2>
class TestResolveClass
{
public:
template< TReturn (TObj::*Func)(TParam1, TParam2 ) >
inline static void funcThatFailsResolve(TObj* obj)
{
(void)obj;
(void)Func;
}
};
template< class TSpecialHandler, class TParent >
class Chain :
public TParent
{
public:
typedef TParent Parent;
typedef TestResolveClass<void, SpecialHandler<Test1, 1>, float, int> TestResolver1;
typedef TestResolveClass<void, SpecialHandler<Test2, 2>, float, int> TestResolver2;
typedef TestResolveClass< void, TSpecialHandler, float, int > TemplatedTestResolver;
void traverseChain()
{
Parent* parentPtr = static_cast<Parent*>( this );
parentPtr->traverseChain(); // up the chain we go
SpecialHandler<Test1, 1> testaroo;
TestResolver1::funcThatFailsResolve< &SpecialHandler<Test1, 1>::handlerFunc >( &testaroo ); // no error
TemplatedTestResolver::funcThatFailsResolve< //<-- Reference to overloaded function could not be resolved; did you mean to call it?
&TSpecialHandler::handlerFunc
>( &m_handler );
TemplatedTestResolver::funcThatFailsResolve< // <-- Reference to overloaded function could not be resolved; did you mean to call it?
static_cast<void (TSpecialHandler::*)( float, int )>( &TSpecialHandler::handlerFunc )
>( &m_handler );
}
private:
TSpecialHandler m_handler;
};
template<>
class Chain< NullType, NullType >
{
public:
void traverseChain(){}
};
typedef Chain< NullType, NullType > ChainLink0;
typedef Chain< SpecialHandler<Test1, 1>, ChainLink0 > ChainLink1;
typedef Chain< SpecialHandler<Test2, 2>, ChainLink1 > ChainLink2;
typedef ChainLink2 FullChain;
class ChainContainer :
public FullChain
{
public:
ChainContainer()
{
traverseChain();
}
};
int main()
{
ChainContainer test;
(void)test;
}
Thanks for your help.
Update 2: I changed a few of the names to be more useful and restructured the code to be clear where the problem is. I also placed some example code before the error to show a situation in which the error does not appear.

Template template parameter errors in MSVC, but not Clang. Why?

I have written this code to help me sort indices that refer to a collection, according to some predicate:
#include <algorithm>
#include <functional>
#include <vector>
template<template<class> class Pred = std::less>
struct element_is_pred
{
template<class C>
struct type : private Pred<typename C::value_type>
{
typedef Pred<typename C::value_type> Base;
C const *c;
type(C const &c, Base const &pred = Base())
: Base(pred), c(&c) { }
bool operator()(
typename C::size_type const i,
typename C::size_type const j) const
{ return this->Base::operator()((*c)[i], (*c)[j]); }
};
};
template<template<class> class P, class C>
static element_is_pred<P>::template type<C const> element_is(
C const &c,
P<typename C::value_type> const &pred = P<typename C::value_type>())
{
return typename element_is_pred<P>::template type<C const>(c, pred);
}
and I'm using it like this:
int main()
{
std::vector<size_t> temp;
std::vector<size_t> indices;
indices.push_back(0);
std::stable_sort(
indices.begin(),
indices.end(),
element_is<std::less>(temp));
}
and when I compile it with Clang 3.2:
clang++ -fsyntax-only Test.cpp
it compiles fine.
But when I try to compile it with Visual C++ 2013, I get tons of errors, like:
test.cpp(23) : warning C4346: 'element_is_pred<Pred>::type<const C>' : dependent name is not a type
prefix with 'typename' to indicate a type
test.cpp(23) : error C2146: syntax error : missing ';' before identifier 'element_is'
test.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Which compiler is correct?
What is the correct way to write the code to do this?
GCC gives the following error:
error: need 'typename' before 'element_is_pred<Pred>::type<const C>' because 'element_is_pred<Pred>' is a dependent scope
Following that advice, I can get the program to build on GCC by prepending typename:
static typename element_is_pred<P>::template type<C const> element_is(
^^^^^^^^
Clang allows the modified version as well.

expected ';' after expression

I'm trying to port a project from Windows to Mac. I'm having troubles compiling the class CFactory. I'll try to illustrate the problem.
Here's what I have on my Factory.h
namespace BaseSubsystems
{
template <class T>
class CFactory
{
protected:
typedef T (*FunctionPointer)();
typedef std::pair<std::string,FunctionPointer> TStringFunctionPointerPair;
typedef std::map<std::string,FunctionPointer> TFunctionPointerMap;
TFunctionPointerMap _table;
public:
CFactory () {}
virtual ~CFactory();
}; // class CFactory
template <class T>
inline CFactory<T>::~CFactory()
{
TFunctionPointerMap::const_iterator it = _table.begin();
TFunctionPointerMap::const_iterator it2;
while( it != _table.end() )
{
it2 = it;
it++;
_table.erase(it2);
}
} // ~CFactory
}
When I'm trying to compile, the compiler complains:
Factory.h:95:44: error: expected ';' after expression [1]
TFunctionPointerMap::const_iterator it = _table.begin();
^
;
Why is this happening? That am I missing?
NOTE: This project compiles properly on MSVC.
Thanks.
You are missing the required typename keyword when referring to a dependent type. Microsoft Visual Studio incorrectly accepts your code without the typename (this is a well known mistake in VS that will never be corrected).
typename TFunctionPointerMap::const_iterator it;

Is the code in "The C++ Programming Language Third Edition" on page 854 correct?

I try to learn C++. In "The C++ Programming Language Third Edition" book I found code on page 854 (Appendix C.13.1):
template<class T> class X {
static T def_val;
static T* new_X(T a = def_val);
};
template<class T> T X<T>::def_val(0, 0);
template<class T> T* X<T>::new_X(T a) { /* ... */ }
template<> int X<int>::def_val<int> = 0;
template<> int* X<int>::new_X<int>(int i) { /* ... */ }
I modify it:
template<class T> class X {
static T def_val;
static T* new_X(T a = def_val);
};
template<class T> T X<T>::def_val(0, 0);
template<class T> T* X<T>::new_X(T a) { return new T(a); }
template<> int X<int>::def_val<int> = 0;
template<> int* X<int>::new_X<int>(int i) { return new int(i); }
But my compiler won't compile it:
1>main.cpp(15): error C2143: syntax error : missing ';' before '<'
1>main.cpp(15): error C2988: unrecognizable template declaration/definition
1>main.cpp(15): error C2059: syntax error : '<'
1>main.cpp(16): error C2143: syntax error : missing ';' before '<'
1>main.cpp(16): error C2470: 'X<T>::new_X' : looks like a function definition, but there is no parameter list; skipping apparent body
1> with
1> [
1> T=int
1> ]
1>main.cpp(16): error C2988: unrecognizable template declaration/definition
1>main.cpp(16): error C2059: syntax error : '<'
1>main.cpp(19): error C2143: syntax error : missing ';' before '{'
1>main.cpp(19): error C2447: '{' : missing function header (old-style formal list?)
1>
1>Build FAILED.
What wrong code in the book or compiler?
The code from the book is wrong. The last two lines should read:
template<> int X<int>::def_val = 0;
template<> int* X<int>::new_X(int i) { return new int(i); }
Don't forget to mail Stroustrup. From his homepage:
"As a small token of my gratitude to
people who report problems and thereby
help me improve the book, I offer a
reward to someone who first reports a
misspelling, a bug in a code example,
or a factual error in the text to me.
The bounty for new errors in the text
is US$32."
Edit: Oops. You are too late. The bug was fixed in the 19th printing. See here. So no bounty, sorry!
The corrected version looks like this:
template class X {
// ...
static T def_val;
static T* new_X(T a = def_val);
};
template< class T> T X<T>::def_val; // initialize to X<T>()
template< class T> T* X<T>::new_X(T a) { /* ... */ }
template< > int X<int>::def_val = 0;
template< > int* X<int>::new_X(int i) { /* ... */ }
You want
template<class T> class X {
static T def_val;
static T* new_X(T a = def_val);
};
template<class T> T X<T>::def_val(0, 0);
template<class T> T* X<T>::new_X(T a) { return new T(a); }
template<> int X<int>::def_val = 0;
template<> int* X<int>::new_X(int i) { return new int(i); }

How to fix 'expected primary-expression before' error in C++ template code?

Here's yet another VC9 vs. GCC 4.2 compile error problem. The following code compiles fine with VC9 (Microsoft Visual C++ 2008 SP1) but not with GCC 4.2 on Mac:
struct C
{
template< typename T >
static bool big() { return sizeof( T ) > 8; }
};
template< typename X >
struct UseBig
{
static bool test()
{
return X::big< char >(); // ERROR: expected primary-expression
} // before 'char'
};
int main()
{
C::big< char >();
UseBig< C >::test();
return 0;
}
Any ideas how I can fix this?
That should be
return X::template big< char >();
Dependent names from templates are taken to not be types unless you specify that they are via typename and assumed to not be templates unless specified via template.