C/C++ :: Explicit Initialization Of Function Templates
Feb 28, 2013
Why will i use explicit instantiation of a template, for a type? If I do not use explicit instantiation, the template is used to create the necessary function then what is the use of explicit instantiation?
template <class Any>
void Swap (Any &, Any &);
// template prototype
template <> void Swap<job>(job &, job &);
// explicit specialization for job
[Code] ....
In the above eg. explicit instantiation is done for char type. What is the use of this?? If the compiler can make use of the template to make a fn for char type.
<eg. taken from C++ Primer>
View 4 Replies
ADVERTISEMENT
Feb 21, 2014
I was just wondering how is this generally resolved. Let say you have this large function that runs in two modes. In the first mode it evaluates the data passed to a function as a map the the second mode it fills the map. example:
Code:
template <typename Map, typename Int>
void func(Map & map, Int i){
int z = 0;
string zz;
[Code] ....
The point is i do not want to write a large function just to include different modes so i decided to set "i" to be a mode identifier. However when i want to compile my function given two modes i get an error since the modes are not recognized (obviously). if i pass map as
Code: map<int,int>
and mode 1 i get an error here :
Code: map[z] = z; besause map
Code: map[z] expects z to be an int not string and the other way around (though in practice this cannot happen since i set the modes). So am i restricted to writing my function for both modes separately (polimorf.) or there is a way to make my example work.
View 1 Replies
View Related
Jun 21, 2013
i'm new to C++ i came across this bubblesort program earlier relating to class templates i was wondering how to make this into simple function templates
#include<iostream>
#include<iomanip.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
template <class t>
class bubble
[Code] ....
View 3 Replies
View Related
Mar 5, 2014
template <class T>
void Arreglo<T> :: Registro (ifstream& Entrada) {
Entrada >> Cantidad;
Dealer = new T [Cantidad];
for (int i = 0; i < Cantidad; i++) {
[Code] .....
It says the following error when I comile it:
error: expected type-specifier before 'Detail'
(*(Dealer + i)).Modelo = new Detail[(*(Dealer + i)).AmountModels];
error: expected ';' before 'Detail'
View 3 Replies
View Related
May 22, 2014
I am working on Explicit Linking of a DLL to an executable. But, when it comes to application, I have some troubles.
1) I try to export a class using C Language approach. (I would ask for other approaches later).
My dll project as follows:
dllmain.cpp
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
[code]....
2) Related with the question above, what is the best way to provide a "pure" explicit linking? How can I explicitly link a dll without introducing the corresponding .lib and without including the dll's header file?
View 8 Replies
View Related
Apr 28, 2013
I would like to initialize an arry containing function pointers with adresses of functions that have a variable number of arguments.
Below the code that works in principle. I would however get rid of the warning message during compilation pointing to the initialzation of the funtion pointers in the array. How do I need to cast the pointers to the functions ?
Code:
gcc func_ptr_init.c
func_ptr_init.c:28: warning: initialization from incompatible pointer type
func_ptr_init.c:32: warning: initialization from incompatible pointer type
Code:
#include<stdio.h>
unsigned char func1_ptr(unsigned int* if_desc, int* result_code) {
*if_desc = 1;
*result_code = 1;
return(0);
[Code] ....
View 8 Replies
View Related
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
Jan 31, 2013
I am porting code from windows using visual studio to mac with xcode.
Quite a lot of issue have a appeared, which is no surprise, one warning that keeps on appearing is Explicit Specialiszation cannot have a storage class:
enum SortReturnCodeEnum {LOWEST=1, HIGHEST=2, HIGHLOW = 3, IDENTICAL=4};
template<typename tKey>
static SortReturnCodeEnum DefaultCompare(typename ArgType<tKey>::ARGT Value1, typename ArgType<tKey>::ARGT Value2)
{
[Code]....
I could do a #define __GNUC__ but i was checking
View 3 Replies
View Related
Apr 13, 2013
I've written the following code and keep getting the errors:
Error1error C2244: 'Supermarket<temp>::operator =' : unable to match function definition to an existing declaration
Error2error C2244: 'Supermarket<temp>::setName' : unable to match function definition to an existing declaration
Error3error C2244: 'Supermarket<temp>::setArea' : unable to match function definition to an existing declaration
#ifndef SUPERMARKET_H
#define SUPERMARKET_H
#include<string>
#include<iostream>
using namespace std;
[Code] .....
I moved the files to the .h file, and now I'm getting
Error2error LNK1120: 1 unresolved externals
View 11 Replies
View Related
May 10, 2013
following code that I'm reading out of the book "The C++ Standard Library".
class C
{
public:
explicit C(const std::string & s); // explicit(!) type conversion from strings.
...
[Code].....
Now I understand that they are saying that an explicit conversion isn't allowed but what I don't understand is what explicit conversion would be happening if there was one allowed.
Specifically I don't understand the const C & elem syntax. How does this work since the collection holds strings. What would be the syntax for how this:
const C & elem
gets strings. I was thinking it was a class reference that someone how converts to a constructor function pointer or something but i'm really confused.
View 4 Replies
View Related
Jun 26, 2013
I initialized a nested vector with the following code as:
Code:
vector<vector<Point> > vectorB(4, vector<Point> (int(vectorA.size()), 0));
And came across the following error during link stage:
"/usr/include/c++/4.6/bits/stl_vector.h:1080:4: error: no matching function for call to ‘std::vector<cv::Point_<int> >::_M_fill_initialize(std::vector<cv::Point_<int> >::size_type, int&)’ "
View 6 Replies
View Related
Jan 22, 2013
Some reading through C++ code I come across template deceleration without any type , I have tested the code it compiles too
Code:
template<>
class abc {
}
similarly template usage
Code:
abc<> a;
example of such usage can be found here [URL] ....
View 5 Replies
View Related
May 27, 2014
I decided to make a linked list program using classes and templates.I implemented my linked list and added some basic functions, everything worked just fine. Then I proceeded to add templates...First, the error list:
#pragma once
template<class T>
class Node {
private:
Node* next;
[code]....
To sum up: Something's wrong with the templates that I used in "List.cpp" and what it could be.
View 5 Replies
View Related
Oct 25, 2013
I'm currently learning templates -- & my logic is in a knot with what I am trying to do which is the following:
-Create a function name load
-Accepts a filename (the filename is a text file of integers)
-Open the file
-Create an array(dynamically allocating an array) filling it with the elements read in from the file & returns the array(so that the return type of the array is a pointer to the element type of the array).
//Header file:
#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H
#include <iostream>
template <typename T>
void load(std::string filename, T *&arr, int *size);
[code].....
how to allocate memory when it comes to using templates..
View 2 Replies
View Related
Mar 14, 2013
After asking the user to input 2 values i expect my program to read them and then output whether they are equal, bigger or smaller than one another. However i am getting no output what so ever when i input the values
code below:
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;
int intInput1, intInput2;
float floatInput1, floatInput2;
int x, n;
[Code] .....
View 1 Replies
View Related
Dec 15, 2013
Let us consider the following two classes:
class case1
{
public:
int n;
float x;
[code]....
Now, let's suppose that I instantiate an array of 100 elements of each class:
case1 c1[100];
case2<20> c2[100];
My question is this: will c2 occupy roughly half of the memory than c1?will the value c1.n be allocated 100 times, while the value c1.n wil be allocated only once?
View 3 Replies
View Related
Aug 26, 2014
Why wouldn't this code compile when adding '<double>' after the function call?
template<class T>
T add(T& a, T& b){
return a + b;
}
int main() {
int a, b;
cin >> a >> b;
cout << add<double>(a,b);
}
Compiler error:
cannot convert 'a' (type 'int') to type 'double&'
View 2 Replies
View Related
Feb 25, 2013
How templates are implemented internally?
Theoretically templates are nothing but a generic routines. But how it has been implemented internally?What complier exactly does?
View 3 Replies
View Related
Mar 14, 2013
Having issues calling my arguments from my templates, when i declare x and n in the main it comes up with errors
code below:
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;
int intInput1, intInput2;
[code].....
View 4 Replies
View Related
Mar 6, 2014
Suppose you have a templated class, such as
template <typename T>
class Matrix {
// some stuff and some methods
};
and let's say that you have some methods that need to do some type-dependent stuff, like, for example,
template <typename T>
Matrix<T> Matrix<T>::transpose() const {
// get this->rowCount, this->columnCount
// create a Matrix that has rowCount amount of columns and columnCount amount of rows
// copy (*this)[j][k] to theMatrix[k][j] (for all of the entries in *this)
// if the entries are complex, take the complex conjugate of them all
}
Would it be good practice to check explicitly for the typename parameter (or is this, somehow, defeating the purpose of templates)? std::cout << "I know that this is a design question, but it needs to be asked... ";
View 1 Replies
View Related
Feb 16, 2015
What I'm trying to do is create a class for constructing an 'op tree' for parsing infix notation.
I started with a base class that uses a map of lambdas to actually calculate the operations (since they are mostly 1 line functions) of passed in integer or float values.
This base class just uses a templated T type as the lvalue and rvalue. I realized though that if I overload the math operators, +, -, etc.. I could also use the class itself as a type for the lvalue and rvalue. This lead me to think I could easily create the op tree by using Operation class members themselves as operands, which I think makes sense but I'm having some trouble expressing the code.
Heres what I have thus far
Code:
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
namespace Calc {
[Code] .....
Example, if you look at the main() function I create normal operations easily with integer values. I then try to create a "tree" operation that includes 2 sub-operations as it's rvalue and lvalue, that is where I'm having some conceptual problems as far as implementing the code to do that.
View 2 Replies
View Related
Oct 27, 2014
I am trying to implement BST using templates. This is the code that i have written.
Code: template <class T>
struct node
{
struct node *left;
[Code].....
There are no errors but the program hangs. What is the mistake with this program ?
View 2 Replies
View Related
May 7, 2013
For my project I have to sort 5 numbers and 5 names using a template bubble sort. I have one header for the numbers, and one for the names. This is what I have so far for my testing page:
#include "Floatheader.h"
#include "Nameheader.h"
#include <string>
int main ()
myFloat obj1;
myFloat obj2( 2.2, 5.1);
[Code] .....
I have to create a template to look like this: template<>....with a class inside the arrows. Then, I have to use bubble sort to sort the 5 names and number objects I have created. Sorting the names and numbers and also using templates?
View 1 Replies
View Related
Feb 10, 2014
A static method named readFromFile that takes a C-string as the first parameter, and an orderedLinkedList<MemberDO> object as the second parameter. The first argument is the filename that contains data for existing members. This method should read the data for each individual member from the input file (one line of data per member), create a new MemberDO object, and insert this object into the linked list specified in the second argument.
How do I take the second parameter in, do I need to create the Linked List first? Here is the code I have so far
#include<iostream>
#include<string>
#include <fstream>
using namespace std;
class MemberDO {
[Code] ....
What to do with the readFromFile static method?
View 1 Replies
View Related
Oct 3, 2014
I want to create an abstract base class having a member function that can accept a templatized structure as its parameter, something that according to C++'s rules can't be done for a good reason.
That good reason it is because an abstract base class is intended to provide interface rules to the classes that will derive from it and should not deal with data.
But how would you go about doing something like the following which is probably a reasonable design decision?
template<typename T>
class Matrix { /* ... */ };
class iFile {
public:
virtual ~iFile() {} = 0;
virtual void Process(const Matrix<T>&) = 0;
[Code] .....
View 5 Replies
View Related
May 1, 2014
I am having problems implementing ArrayList using templates. I was given a program and I have to create this implementation to make it work. It keeps giving me an error "invalid operands to binary expression" .....
ifndef Final_4_ArrayList_hpp
#define Final_4_ArrayList_hpp
#define MAX 10;
#include "List.hpp"
template <class T>
class ArrayList : public List <T> {
[Code] ....
View 1 Replies
View Related