This question already has answers here:
How does the compilation of templates work?
(7 answers)
Closed 5 years ago.
what happen when a class template instantiate . i.e i hv a class template and i have created class for int and class for float, so what will happen at compile time( compiler will create 2 seprate class for int and float) or not?
eg :
template <typename T>
class A
{
public:
void foo(T t)
{
//...
};
};
int main()
{
A<int> a;
A<float> b;
}
Yes, the compiler will create two new classes on-the-fly - one for int and one for float.
Related
This question already has answers here:
What are some uses of template template parameters?
(10 answers)
Closed 2 years ago.
I have some code:
template<typename Container = std::vector> // there is error
class SomeClass {
struct SomeDataStructure {
int a;
float b;
};
Container<SomeDataStructure> container; // and there
};
I want to pass template class type in template arguments of other template class.
In this example, I want to pass std::vector to SomeClass and define Container(std::vector) of private structure SomeDataStructure.
But I'm getting errors(MSVC compiler) C2059 and C2238.
How can I solve it?
Thanks in advance!
You need template template parameters:
template<template<class, class...> class Container = std::vector>
This question already has answers here:
Derived template-class access to base-class member-data
(3 answers)
Why doesn't a derived template class have access to a base template class' identifiers?
(4 answers)
Why do I have to access template base class members through the this pointer?
(3 answers)
Closed 3 years ago.
Why using std::conditional prevents a child struct to access public members of a parent struct? As a case in point, the following program doesn't compile, complaining that a is not defined. What would be the right way to fix this?
template <int i>
struct mtmd_A {
static constexpr int a = i;
};
template <int i>
struct mtmd_B {
static constexpr int a = i * i;
};
template <bool select>
struct mtmd: public std::conditional<select, mtmd_A<17>, mtmd_B<17>>::type {
static constexpr int b = a;
};
int main()
{
std::cout << mtmd<true>::b << std::endl;
}
Edit
This doesn't seem to be a duplicate of this question since the problem appears only when I add std::conditional.
This question already has answers here:
Why doesn't a derived template class have access to a base template class' identifiers?
(4 answers)
Closed 7 years ago.
The following code
template<int c>
struct Base
{
static const int a = c + 5;
};
template<int c>
struct Derived : Base<c>
{
static const int b = a + 5;
};
... fails to compile because a was not declared in this scope. Explicitly specifying Base<c>::a works, but logically this shouldn't be necessary because we're deriving from Base<c>. Is this intended behaviour (and why) or am I missing something?
template<int c>
struct Derived : Base<c>
{
static const int b = Base<c>::a + 5;
};
It really depends on compiler, but gcc requires it, it plays stupid when it comes to template classes
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ Restrict Template Function
Is it possible to write a C++ template to check for a function's existence?
Is it possible to restrict the types for which a template may be instantiated
(that is, have a compiler error if I use template<type_not_allowed>)?
One way would be to provide no default implementation, and specialize your class template only on those types you wish to allow. For example:
#include <iostream>
using namespace std;
template<class X> class Gizmo
{
public:
Gizmo();
};
template<> Gizmo<int>::Gizmo()
{
}
int main()
{
Gizmo<float> gf; // ERROR: No specialization for Gizmo<float> results in a linking error
}
Make the constructor private in the illegal type:
template<typename T>
class Z
{
public:
Z() {}
};
template<>
class Z<int>
{
private:
Z();
};
Z<float> f; // OK
Z<int> i; // Compile time error.
This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
problem with template inheritance
This code doesn't compile in GCC:
template <typename T>
struct Base
{
public:
int x;
};
template <typename B>
struct Middle : public B
{
};
template <typename T>
struct Sub : public Middle<Base<T> >
{
public:
void F()
{
x=1; // error: ‘x’ was not declared in this scope
}
};
If either Base or Sub weren't template classes, it wouldn't complain. VC handles it.
Why?
Use this->x = 1; to tell the compiler that x is a (template-) dependent name.
Note: What GCC does ot fine according to the standard, MSVC is just a bit more tolerant.