templates question

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 329 Views - Last Post: 09 February 2012 - 05:45 AM Rate Topic: -----

Topic Sponsor:

#1 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

templates question

Posted 08 February 2012 - 07:56 AM

#include <iostream>
#include <string>

template<int32_t Exponent, int64_t Mantissa>
struct FieldDecimalOptionalState
{
    FieldDecimalOptionalState():exponent_(Exponent), mantissa_(Mantissa)
    {
    }
    int32_t exponent_;
    int64_t mantissa_;
};

template<int32_t Exponent, int64_t Mantissa ,FieldDecimalOptionalState<Exponent,Mantissa> *member>
struct OptionalFieldDecimal
{
    OptionalFieldDecimal(): value(0)
    {
    }
    OptionalFieldDecimal(std::string const& dec): value(dec)
    {
    }
    std::string value;
};

int main()
{
    FieldDecimalOptionalState<5,6> fos;
    OptionalFieldDecimal<5,6,&fos> ofd; // edited.
    return 0;
}


I am getting these errors.
temp.cpp: In function âint main()â:
temp.cpp:29: error: template argument 3 is invalid
temp.cpp:29: error: invalid type in declaration before â;â token

What is wrong in my code????

This post has been edited by bnc: 08 February 2012 - 09:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: templates question

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 962
  • View blog
  • Posts: 2,359
  • Joined: 04-December 09

Re: templates question

Posted 08 February 2012 - 08:19 AM

Template parameters must be COMPILE-TIME constants. That means you cannot use the name of a variable (sure, doesn't include case where the value is const, and is assigned a rvalue, e.g const int x = 34;, in this case x can be a template parameter) or (as in your case), the address of a variable as a template argument.

This post has been edited by sarmanu: 08 February 2012 - 08:23 AM

Was This Post Helpful? 1
  • +
  • -

#3 Toadill  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: templates question

Posted 08 February 2012 - 08:32 AM

There really is no difference between a structure and a class besides that everything in a struct is automatically public and a class everything is automatically private.

//this is a function example

//start function using template
//This function returns the template type Type and accepts to template parameters of type Type.
//Basically what is going on here is we are passing the data type into the function so it knows how to calculate.
template <class Type> // This declares the name of the data type to be Type
Type larger(Type x, Type y)
{
  if (x >= y)
    return x;
  else 
    return y;
}



#include <iostream>

using namespace std;

template <class Type>
Type larger(Type x, Type y);

int main()
{
//Here we are passing type integer to the larger function
 cout << "Line1: Larger of 5 and 6 = " << larger(5, 6) << endl;
//Here we are passing type char to the larger function
 cout << "Line2: Larger of A and B = " << larger('A', 'B') << endl;
//Here we are passing type double to the larger function
 cout << "Line3: Larger of 5.6, 3.2 = " << larger(5.6, 3.2) << endl; 
}


A class or struct would be the same except below the template declaration we include methods and local variables.
I hope this helps

btw it is a little confusing why you have comma's between your template types. Are these all suppose to be a type of there own?

template<int32_t Exponent, int64_t Mantissa>



What a template does is allow multiple data types for example
a program that asks a user to enter a favorite number, color or letter may except, integer, string, and char. The point of the template is to easily except whatever is entered

This post has been edited by Toadill: 08 February 2012 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

Re: templates question

Posted 08 February 2012 - 08:40 AM

@Toadill

You should seperate the template parameters using comma.
Otherwise we will get errors.

You mean to say

we should declare like this???

template<int32_t Exponent int64_t Mantissa>



@sarmanu

then how can we overcome my problem... Passing an address to the template pointer...

Is it not possible?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 Toadill  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: templates question

Posted 08 February 2012 - 08:44 AM

No what I am saying is that you need to use the keyword class or you could use the keyword typename with templates as far as I have learned I never seen a template accept two different template types, like you have displayed the class and typename are keywords used to declare a template data type in my example I name it Type

Could you explain what exactly you want your program to do please?

This post has been edited by Toadill: 08 February 2012 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 1893
  • View blog
  • Posts: 5,683
  • Joined: 25-December 09

Re: templates question

Posted 08 February 2012 - 08:56 AM

The int32_t and int64_t are not template parameters, they are typedefs for 32 bit and 64 bit ints.

I don't really see the need to use templates in the presented code, there are no template parameters being shown.

Jim

This post has been edited by jimblumberg: 08 February 2012 - 08:57 AM

Was This Post Helpful? 0
  • +
  • -

#7 Toadill  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: templates question

Posted 08 February 2012 - 08:59 AM

Well okay this is why I was confused I never seen a template used in this way, I am not even sure if it is possible. I know very little about Win32 for C++, since my focus is DirectX game programming, but we have used templates.
Was This Post Helpful? 0
  • +
  • -

#8 Toadill  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: templates question

Posted 08 February 2012 - 09:05 AM

The four supported types are int32_t, uint32_t, int64_t, and uint64_t. The four generating functions are:

//C
    struct libdivide_s32_t libdivide_s32_gen(int32_t  y)
    struct libdivide_u32_t libdivide_u32_gen(uint32_t y)
    struct libdivide_s64_t libdivide_s64_gen(int64_t  y)
    struct libdivide_u64_t libdivide_u64_gen(uint64_t y)



Maybe this relates to what you are trying to accomplish?

link to more info

This post has been edited by Toadill: 08 February 2012 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon

Reputation: 1893
  • View blog
  • Posts: 5,683
  • Joined: 25-December 09

Re: templates question

Posted 08 February 2012 - 09:08 AM

Quote

I know very little about Win32 for C++

Those typedefs (int32_t, int64_t) are part of the C standard that C++ supports. See this link: cstdint.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

Re: templates question

Posted 08 February 2012 - 09:08 AM

When I comment this line..

OptionalFieldDecimal<5,6,&fos> ofd;



It is not giving any compilation issues...
Was This Post Helpful? 0
  • +
  • -

#11 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

Re: templates question

Posted 08 February 2012 - 09:26 AM

Can we change the line like this... If there is a problem in passing reference to a template parameter

template<int32_t Exponent, int64_t Mantissa ,FieldDecimalOptionalState<Exponent,Mantissa> member>
struct OptionalFieldDecimal
{
    OptionalFieldDecimal(): value(0)
    {
    }
    OptionalFieldDecimal(std::string const& dec): value(dec)
    {
    }
    std::string value;
};




Thanks
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 621
  • View blog
  • Posts: 1,038
  • Joined: 21-June 11

Re: templates question

Posted 08 February 2012 - 09:29 AM

View Postbnc, on 08 February 2012 - 04:40 PM, said:

then how can we overcome my problem... Passing an address to the template pointer...

Is it not possible?


Right, it is not possible to pass pointers to local variables as template arguments. You'll need to pass the pointer as an actual run-time argument.

As jimblumberg pointed out, it is not clear why you're trying to use templates at all. It doesn't seem necessary or even sensible here. Especially since you don't actually use member anywhere.
Was This Post Helpful? 0
  • +
  • -

#13 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

Re: templates question

Posted 08 February 2012 - 09:33 AM

@sepp2k

can you give me an source over web to get more grip on templates... which you think is more useful... I am asking bcoz you may have come across many tutorials.. I am also finding some but nobody is explaining them in deep.



Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is online

  • Programmer
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,085
  • Joined: 19-February 09

Re: templates question

Posted 08 February 2012 - 11:21 AM

Are you sure you need templates? You can add the data so :-



struct FieldDecimalOptionalState
{
    FieldDecimalOptionalState(int32_t Exponent, int64_t Mantissa) 
                 : exponent_(Exponent), mantissa_(Mantissa)
    {
    }
    int32_t exponent_;
    int64_t mantissa_;
};


int main()
{
    FieldDecimalOptionalState fos(5,6);

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#15 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 102
  • Joined: 18-March 10

Re: templates question

Posted 09 February 2012 - 05:16 AM

@#define

I know I can do like that.
This is a sample program.

I have a doubt in this program. Is it legal to take a pointer in template declaration?

Thanks.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2