C++ :: Variadic Templates And Overload Resolution

Jan 2, 2014

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.

View 4 Replies


ADVERTISEMENT

C++ :: Variadic Templates And Type Composition?

Jan 28, 2014

Let's say I have a variadic type A and a non-variadic type B:

Code:
template <typename ... Args>
class A
{};
template <typename T>
class B
{};

Now, I'd like to do the following (pseudocode since I don't know how to do this yet):

Code:
template <typename ... Args>
class C: public A<B<Args1>,B<Args2>,B<Args3>....>
{};

Essentially, I'd like to take the parameter pack used for C and compose B around each element individually, then pass the result to A.

how I can do this?

View 2 Replies View Related

C++ :: Variadic Templates - Enter Unlimited Amount Of Types In Function

Feb 25, 2013

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 tried this, but the overload is ambiguous?

#include <iostream>
#include <typeinfo>
template<typename T>
void stuff() {
std::cout << typeid(T).name() << "

[Code] ....

View 2 Replies View Related

C++ :: No Constructor Could Take Source Type Or Constructor Overload Resolution Was Ambiguous

Mar 1, 2014

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{

[code]....

View 1 Replies View Related

C++ :: Scope Resolution Operator?

Feb 21, 2013

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?

View 11 Replies View Related

C++ :: Name Resolution Not Working As Expected

Apr 23, 2013

I have a global var (m) with an initial value 5.

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

View 2 Replies View Related

C/C++ :: Setting Resolution On Screen

Mar 29, 2015

Just reading some code and come across a section where its setting the resolution on a screen

so like

image->sizeX=640;
image->sizeY = 480;
size = image->sizeX * image->sizeY *3;
float sizeLog = ceil(log((float)image->sizeX) / log(2.F)));

So yea, my understanding of this code is that firstly there is member access and that a resolution is being set.

And then its like 640 * 480 * 3

and then the log of 640 *480*3 ...being divided by something else and being rounded up by ceil

is this the right way to look at it?

and also what is the log(2.F) - i don't really understand that.

View 3 Replies View Related

Visual C++ :: MFC Dialog Clipping Off In Low Resolution

Mar 25, 2013

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!

View 1 Replies View Related

Visual C++ :: How To Find Printer Resolution From GetDeviceCaps

Dec 6, 2012

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.

View 2 Replies View Related

C :: Variadic Macro Function

Sep 12, 2013

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:

Code:

#define alloCpy(format, ...) _alloCpy(format, ##__VA_ARGS__, NULL);
char* _alloCpy(char *format, ...); mycfile.c: Code: char* _alloCpy(char *format, ...) {
va_list args;
va_start(args, format);
int args_len = 0;
}

[code]....

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?

View 3 Replies View Related

C++ :: Inheriting One Variadic Class From Another?

Jan 30, 2014

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.

View 2 Replies View Related

C++ :: VGA Resolution Limit With 1bpp Mono Graphics Modes?

Aug 25, 2014

What is the limit of a VGA with 256k VRAM, running 1bpp monochrome graphics? How high can the display resolution go?

View 3 Replies View Related

C++ :: Template Function Overloading Resolution Different In Visual Studio And GCC

Jun 13, 2014

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.

View 2 Replies View Related

Visual C++ :: Free Image Library That Handle PNG Resolution

Mar 14, 2013

Need a free image library that can handle png xresolution and yresolution. I need it to be compatible with visual studio 6.0

View 1 Replies View Related

C++ :: Variadic Template Basic Syntax

Jul 31, 2014

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.

[Code] ....

View 3 Replies View Related

C++ :: How To Remove Last Argument In Variadic Template

Nov 1, 2013

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)

View 8 Replies View Related

C++ :: Enable If With Variadic Template Arguments

Sep 17, 2014

template <typename FIRST, typename... REST, typename std::enable_if<std::is_convertible<FIRST, Base*>::value>::type* = nullptr>
void foo (FIRST first, REST... rest) {}

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?

View 7 Replies View Related

C++ :: Passing Variadic References To Template Function?

Dec 14, 2014

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;

[code].....

View 4 Replies View Related

C++ :: Variadic Function - Parameters Get Passed In Reverse

Nov 17, 2014

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

Code:
#include <iostream>
#include <stdarg.h>

void foo_func(int v1, int v2)
{
std::cout << v1 << " " << v2 << std::endl;

[Code] .....

View 3 Replies View Related

C++ :: Expanding Variadic Template Arguments In Lambda

Feb 1, 2013

I'm trying to expand a template + argument parameter list inside a lambda function like this:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Perform pre-thread creation work.
std::thread([=]()

[Code] ....

But this does not work:

The compiler error is "error: parameter packs not expanded with ‘...’:|"

However, when I do the following:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Pre-thread work.
auto tthr = [](Args ...ar) -> void {

[Code] ....

It works just fine. That shows that lambda threads are able to take variadic arguments...

So here is my question; what is the correct capture clause for capturing the variadic object correctly?

View 3 Replies View Related

C++ :: Ambiguous Call On Overloaded Variadic Template Operators

Oct 30, 2013

When compiling the code

#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?

View 3 Replies View Related

C++ :: Variadic Template Function Parameters And Method Pointers?

Oct 24, 2013

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:

View 3 Replies View Related

C++ :: How To Overload Operator Twice

Jul 10, 2014

Here's my question. I'm coding a basic Linked List class (for the purpose of understanding and having fun, I know about STL), LList.

I have overloaded the [] operator so it returns the data of the index-th node in the list, for example, if I code

LList x;
....
cout<<x[5];

it prints the data of the 5th node in the list (for fun I decided to index from 1 to infinity).

My question: Now I want to be able to assign the value to the index-th node data, using [] and =, for example, I want to be able to write:

LList x;
.....
x[5] = 121;

How can I do that?

View 3 Replies View Related

C++ :: How To Able To Overload A Multiplication Operator

Apr 24, 2014

How would i be able to overload a multiplication operator that if, in the main example(0, 5, 0) * example (0, 5, 0) is given, it gives me 25?

View 1 Replies View Related

C++ :: Overload With Template Function

Nov 15, 2013

I am a beginner, got confused about:

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?

View 15 Replies View Related

C++ ::  How To Overload Variables In Derived Classes

Apr 6, 2013

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;
}

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved