ISO C++ does not include variadic templates - c++

Good day, guys. I can't really get what's going on with this and where is my mistake? It looks like I am missing small detail, but the code looks okay.
#include <pspkernel.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <string.h>
PSP_MODULE_INFO("CONTROLTEST", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
int done = 0;
template<typename... Args>
void Printf(Args... params)
{
pspDebugScreenPrintf("Test %d\n", params...);
}
int main(void)
{
pspDebugScreenInit();
Printf(1);
while(!done){
}
sceKernelExitGame();
return 0;
}
Error I'm getting looks like this:
main.cpp:12: error: ISO C++ does not include variadic templates
main.cpp:13: error: ISO C++ does not include variadic templates
I'm using gcc 4.3.5

According to the GCC docs, GCC 4.3 added support for variadic templates, but C++0x (which became C++11) was still experimental at that point.
If you really want to use C++11 or newer standards, update to a recent compiler. Specifically, GCC 4.8.1 was the first feature-complete implementation of the 2011 C++ standard, so you should aim at least for that version.

Related

Why does VC++ compile the code while clang doesn't?

I use VS 2015 (Update 3) to compile the following code:
#include <codecvt>
#include <cctype>
#include <functional>
int main()
{
std::function<int(int)> fn = std::isspace;
}
If I use VC++ to compile it, it's ok. However, if I change the compiler to Visual Studio 2015 - Clang with Microsoft CodeGen (v140_clang_c2) in Visual Studio, clang reports an error:
main.cpp(7,26): error : no viable conversion from '' to 'std::function'
std::function fn = std::isspace;
More surprising, if I comments the first line as follows, clang will also be ok.
//#include <codecvt> // now clang feels happy
#include <cctype>
#include <functional>
int main()
{
std::function<int(int)> fn = std::isspace;
}
What's the root cause?
std::isspace is overloaded in the standard library.
Due to the structure of their standard library headers, one compiler sees two different declarations of the name.
Then its use without arguments or casting is ambiguous.
std::isspace is ambiguous, it can either refer to the function found in <cctype> which is for compatibility with C, or the function template found in <locale>.
You can resolve the ambiguity with
std::function<int(int)> fn = static_cast<int(*)(int)>(std::isspace);
Or by omitting the std:: namespace, although technically there's no requirement for implementations to import the C functions into the global namespace.
The Clang and GCC implementations of <codecvt> both seem to include declarations of the template from <locale>, hence the error; presumably VS doesn't.

PRIuPTR preprocessor bug in GCC?

The following program, when compiled as C++ with GCC 4.8.1
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
int main() {
uintptr_t i = 0;
i--;
printf("%" PRIuPTR "\n", i);
return 0;
}
gives the following error message
a.cc: In function 'int main()':
a.cc:8:13: error: expected ')' before 'PRIuPTR'
printf("%" PRIuPTR "\n", i);
^
It compiles and runs correctly with the Microsoft compiler, and even with GCC when compiled as C.
Am I missing something, or is this a bug in GCC? If the latter, is there a workaround?
The C standard says this about including the C header from C++:
C++ implementations should define these macros only when __STDC_FORMAT_MACROS is defined
before <inttypes.h> is included.
and it seems that GCC follows this recommendation, while Microsoft doesn't.
Rather than defining this macro and including the deprecated C header, a better solution is to use the C++ header <cinttypes>, which defines these macros unconditionally. (As noted in the comments, the C++ standard specifically says that the macro has no effect on the C++ header.)
Alternatively, stop using the C library when there's a more convenient (and typesafe) C++ alternative, std::cout << i;

Is there a method to use gmpxx.h together with c++98?

Because of my project I need to use c++98 and gmpxx.h:
But even for a simple project, it doesn't work:
#include <gmp.h>
#include <gmpxx.h>
int main()
{
int xrange=5,yrange=5,component=5;
return 0;
}
The error message is:
I tried using the following compiling methods
libc++: support c++11 and thus work
libstdc++: only support c++98 and do not work
Is there a way to use c++98 to implement gmpxx? thank you :)
Detail of errors when using c98++ to implement:
The breaking line is:
cout<<r<<endl;
But it works in c++11:
The error report:

C++11 typedef alias compile error

When I try to compile this, I get the following error:
error: expected unqualified-id before ‘using’
I know, this was asked several times before, but I didn't find the answer. Usually they say that a semicolon is missing in one of the header files. But it's not the case now. And of course I use the -std=c++0x flag
#include <iostream>
#include <string>
#include <vector>
template <typename T>
using stringpair = std::pair<std::string, T>;
int main (int argc, char* argv[]) {
return 0;
}
Your error is caused by the fact that template aliases with using is a C++11 feature and your compiler does not support it. You should add the corresponding flags at compilation. Those most likely are:
-std=c++11
(at least for g++ and clang++).
Live demo
Otherwise your compiler does not support them yet. GCC supports them from 4.7.

c++ error std::result_of does not name a type

After g++ -std=c++0x'ing std::result_of produces the following error message
error: ‘result_of’ in namespace ‘std’ does not name a type
(g++ version 4.5.0 on SUSE.)
The relevant piece of code, sufficient for reproducing the error is below
#include <random>
#include <type_traits>
using namespace std;
class Rnd{
protected:
static default_random_engine generator_;
};
template<class distribution>
class Distr: Rnd{
distribution distribution_;
public:
typename std::result_of<distribution(default_random_engine)>::type
operator() (){ return distribution_(default_random_engine); }
};
Moreover, I have tried to compile examples from wikipedia or cpluplus.com to no avail.
Is it a problem with the particular compiler or am I doing something wrong?
Try to include <functional> also. gcc 4.5 is based on an older version of C++11, in which std::result_of is defined in <functional> instead of <type_traits>.
This move was introduced in n3090 (2010 March 29) after fixing issue 1270. gcc 4.5.0 was released just 16 days after the change (2010 April 14), which did not have enough time to apply, as we can see from this online source code of <functional>.
std::result_of was moved to <type_traits> in gcc 4.6.