I'm trying to learn how to use variadic templates, and I decided a great example would be serializing a series of types into a stringstream:
Code:
// Send a fully constructed message.
virtual void send(ostringstream &msg) = 0;
// Construct a message from the arguments and send it.
// This is the usual entry point.
template <typename ...Args>
void send(Args ...args {
[Code] ....
This works fine, so far as I can tell. However, I decided to see if I could specialize the way certain types are serialized. I tried using a Google Protocol Buffer object as an example, and added this:
Code:
// Handle a protocol buffer type while constructing a message.
template <typename ...Args>
void send(ostringstream &msg,
const google::protobuf::MessageLite &protobuf, Args ...args) {
std::string msg_str = protobuf.SerializeAsString();
msg << msg_str;
send(msg,args...);
}
I would expect this overload to be preferred over the generic T overload when a protobuf object (which always inherits from MessageLite) is passed into send() anywhere in the list. However, this is not happening. I am getting an error message to the effect that << doesn't know how to deal with my concrete type, pointing at the T overload.
I'm looking for a way to enter an unlimited amount of types in the <> part of a template function, I found Variadic templates but I'm not sure if it can do it, all the examples I've found are similar to the C argument list and don't use the <> part of the template at all.
i am writing this bank accounts program using structures. i haven't implemented the function before that i want to check if the data is being read and printed. When i build and run the program in visual studio it gives me the following error. "No constructor could take the source type, or constructor overload resolution was ambiguous". Now whats wrong in this program?
/* Bank Accounts Program */ #include <iostream> #include <string> #include <fstream> #include <cstdlib>//needed to use system() function using namespace std; const int MAX_NUM = 50; struct Name{
I have done alot of googling for the scope resolution operator and Ive gained a bit of an understanding as to what it does i know it can distinguish between global and local variables, but I see it used to access methods/members of classes such as this example, why not just use a dot instead to access it?:
sql:: Driver *driver;
Why is the scope resolution operator being used here?
I have a template class (A) that derives from a either a base class that has a member (_A1.m) or not (_A0), based upon it's template parameter. class (A) has a member function (fn) returns the value of (m) as it understands what (m) is.
However, this gives different results compared with a non-template class in a similar scenario. I'm expecting that if derived from _A1, that m should be taken from the base class scope and if derived from _A0, it should be taken from the global one.
Here is the code for your amusement:
int m = 5; class _A0 { public: _A0(int) {
[Code] ....
This compiled using g++ 4.5.3 and 4.6.3 with the same results: Global value of m is: 5 B0 class has no internal m member. Object resolves m internally with value 5 B1 class has internal m member. Object resolves m internally with value 3 A<_A0> class has no internal m member. Object resolves m internally with value 5 A<_A1> class has internal m member. Object resolves m internally with value 5
I have an mfc dialog of size 1280 X 1024 and when i change the resolution to 800 X 600, after adding scroll bar, it don't show the entire dialog contents, Seems like it clips the 800 X 600 portion.
I have handled VScroll and HScroll. It works fine for higher resolutions!
I have written a program to capture signal from machine and display it on screen in the form of sinosoidal wave. I have print buffer for 256 X 400 pixel strip chart. This prints fine if I set page size A4 and resolution 600 DPI. If anyone changes print parameters , there is problem. I can resize the printer buffer and interpolate the stored signal.
My problem is , how to find the printer resolution from GetDeviceCaps ? how to know the Print Type Normal / Draft etc in mFC.
I'm doing right now is creating a function that callocs (I prefer this to malloc) and returns a string, and it will work similar to printf, I'm calling the function alloCpy(),I have several values that I need in a malloced string, so I call Code: myAllocedString = alloCpy("Value 1 is %s, value 2 is %s, and value 3 is %d", str1, str2, num); To do this I'm using the Variadic Macro, the reason I'm not just using a Variadic Function such as this: Code: char* alloCpy(char *format, ...) {} is because I need to append NULL to the end for the sake of looping through arguments, and I'm understanding it thusfar, but I have a few issues, first of all, I tried defining the Macro in a header file, but when I try to call it I get the error "Undefined reference to alloCpy". Also, to loop through arguments to get string lengths I'm using va_arg(args, char*) which requires all the arguments to be of type char*. Here is my code: myheader.h:
So, how can I do this to, first of all, make my macro function accessible from other files importing myheader.h, and second, how can I make it accept any type of argument like printf, so that my example above would work?
I have a variadic base class with a pure-virtual function per type:
Code: template <typename ... Types> class Base; template <typename T, typename ... Types> class Base<T,Types...>: public Base<Types...> { public: using Base<Types...>::doSomething;
[Code] ......
Now, I'd like to inherit from it using another variadic class, which provides implementations of doSomething(), but I run into trouble --- where do I indicate it derives from Base?
Code: template <typename ... Types> class Derived; template <typename T, typename ... Types> class Derived<T,Types...>: public Derived<Types...>
[code]....
I see two possible approaches:
First, virtual inheritance *might* get me what I want, but I don't know how bad a performance hit that might be.
Second, I could do some magic where the full set of types is captured in a tuple at the lowest level and continually passed up, then re-expanded in the base case to indicate Base inheritance. However, I'm not sure if that can be done in an unambigious manner; I can't have two variadic packs at once (Types... and the tuple contents), and I'm not sure if there's a way to use enable_if to check if an arbitrary template type is any kind of tuple.
I stumbled upon an unexpected difference between GCC and VisualStudio: Different overloaded functions are called in the following example:
// -------- can assume this is located in 'tool.h' file -------------- // Fwd declaration support foo( const int& ) gets called as expected by both compilers // void foo( const int& n );
template< typename T > void foo( const T& n ) {
[Code] ....
What happens: I expected that by calling bar(1) compiler will notice both versions of foo() and call the best match, in this case foo(const int&). That is not the case.
Note that overloaded foo(const int&) is below bar(). It seems that at that point GCC does not see overloaded version, and happily calls template version. Visual studio on the other hand has no problem finding them both.
If I introduce a forward declaration of foo( const int& ) before bar(), both compilers call that version correctly. Unfortunately, that is not a solution for my problem here.
Template version is part of a library while overloaded is part of the user code. Both would be located in different (header) files and I would not like to impose #include order to the users or to be dependant on it.
I had to learn how to use variadic templates recently, and had trouble finding simple examples that just showed the basic syntax.
So I decided to write one myself. Admittedly, it's a bit on the long side, but that is mostly because it includes five specializations.
insert Code: // Variadic.C // Compile command: g++ Variadic.C -std=c++0x // I used GCC version 4.6.3 on Ubuntu.
// This file contains a basic variadic template with five specializations. // It is intended for non-software engineers who are looking for a simple // example of variadic template syntax.
I wonder if it is possible to remove the last argument in an argument pack? Below is an example on what I want to accomplish:
template<template<int...> class A,int... Ints> A<remove_last_int<Ints...>::list> func(const A<Ints...> & a0) { A<remove_last_int<Ints...>::list> a; ... //Here a set the members of a based on a0. ... return a; }
For example, I want the return a A<1,2> value from (const A<1,2,3> & a0)
that successfully allows me to enable the function foo() only if FIRST is convertible to Base*, but I also only want foo() enabled if each type in REST... meets the same condition. What is the syntax for that? If no such syntax exists, how to achieve that effect?
I'm having some problems in understanding how the code below works and why it produces the output it produces.. What I'd expect is that both functions, namely `add_1' and `add_2', would print the same output; but I've been proven wrong :/ So why does the second one get different memory addresses for the same variable?
Code should be self-explaining:
Code: template<typename... Types> void add_1(Types&&... values) { // by the way: why do i have to use `const int' instead of `int'? std::vector<std::reference_wrapper<const int>> vector{ std::forward<Types>(values)...}; std::cout << "add_1:" << std::endl; for (const auto& value:vector) { std::cout << &value.get() << std::endl;
I noticed that when using variadic functions, if I pass the va_arg() as parameter to a function, the parameters get passed in reverse. Is that expected?
For example, the following code outputs Code: 1 2 2 1
#include "tensor.h" int main() { Tensor<2,-2> m = {{1,2},{1,3}}; Tensor<2> v = {1,5}; std::cout<<m*v<<"
[Code] ....
Why do I get an ambiguity and why is not the wanted operator*-overload (the last one in the tensor.h file) not even mentioned as one of the candidates? Is it clear what I want to do? And if so, what can I do to make the call unambiguous?
I have been experimenting with variadic templates with the aim of caching a call to a class method by storing away the object pointer, method pointer and parameters. I've actually had some reasonable success but have now hit a stumbling block. I now wish to wrap my parameters in a simple template class when I cache them. My success is as follows:
Using variadic template functions to store these pointers and paremeters;
I'm able to pass a method pointer and unwrapped parametersI'm able to pass wrapped parameters on their own.I'm NOT able to pass a method pointer and wrapped parameters I set up a little prototype project to demonstrate the issue and added comments above the function calls to indicate the compilation results. Here is the code:
Code: #include "stdafx.h" ////////////////////////////////////////////////// // Basic class with a simple method ////////////////////////////////////////////////// class MyClass { public: char Method( int i, float f ) { return 'A';
[code]....
But I'm convinced it should take three arguments, the method pointer and two wrapped parameters. Visual studio even suggested it should as shown below:
template<typename T> int compare(T &a,T &b); int compare(const char *a,const char *b); char ch_arr1[6]="world",ch_arr2[6]="hello"; compare(ch_arr1,ch_arr2);
After running the code above,we got to know the non-template function is called. What I know is that the array arguments ch_arr1,ch_arr2 will not be converted to char * because the parameters are references in the template functions.
ch_arr1,ch_arr2 need to be converted to const char * if compare(const char *a,const char *b) were called.
I just wanna know what exactly happened behind that? and why?
Is it possible to overload a variable in a derived class? Example:
struct Circle { int radius() const { return r; } private: int r; } struct Smiley : Circle { // inherits the function int radius() const, but doesn't return Smiley::r private: int r; }