我的 C++ 课程中有一个(应该很简单的)作业。
作业如下:创建一个包含两个私有数据成员的类模板:T * array 和 int size。该类使用构造函数根据输入的大小分配数组。有成员函数允许用户根据大小填充数组。另外,还有一个成员函数对数组进行排序并显示排序后的元素。使用析构函数删除数组。开发main()来创建两个对象来调用成员函数。结果,第一个对象将保存其 double 类型数组,另一个对象将保存其 int 类型数组。
这是我想到的,但出现了“分配不完整类型‘T’”的错误:
#include <iostream>
#include <new>
#include <vector>
using namespace std;
template <class T>
class DynArray {
protected:
int size;
T ** DynamicArray = new T[size];
public:
DynArray(){
void CreateArray(){
cout << "Enter size of Array: ";
cin >> size;
for (int i = 0; i < size; ++i){
DynamicArray[i] = new T();
}
for (int i = 0; i<size; i++) {
cout << "Element " << i …
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码段:
template <typename T>
struct foo
{
foo(T) { }
};
int main()
{
foo{0};
}
Run Code Online (Sandbox Code Playgroud)
g ++ 7愉快地创建了一个类型的临时对象foo
,演绎T = int
.
clang ++ 5和6拒绝编译代码:
Run Code Online (Sandbox Code Playgroud)error: expected unqualified-id foo{0}; ^
这是一个铿锵的错误,还是标准中的某些内容阻止了类模板参数推断对于未命名的临时工具的推动?
template<class Key1, class Key2, class Type> class DualMultimapCache
{
public:
std::list<std::reference_wrapper<Type>> get(Key1 const & key);
std::list<std::reference_wrapper<Type>> get(Key2 const & key);
template<class ...Args> Type & put(Key1 const & key, Args const & ...args);
template<class ...Args> Type & put(Key2 const & key, Args const & ...args);
};
Run Code Online (Sandbox Code Playgroud)
在这里,我有一个类的公共接口.基础数据结构无关紧要.一切都将只是正常工作时Key1
,并Key2
有不同的类型.如果它们最终成为相同的类型,则过载可能是不可能的.我这是对的吗?
如果我是,有没有办法分离过载,同时保持签名尽可能干净?
编辑:这是一个更深入的样本
template<class Key1, class Key2, class Type> class DualMultimapCache
{
public:
std::list<std::reference_wrapper<Type>> get(Key1 const & key);
std::list<std::reference_wrapper<Type>> get(Key2 const & key);
template<class ...Args> Type & put(Key1 const & key, …
Run Code Online (Sandbox Code Playgroud) c++ templates overloading overload-resolution class-template
我正在编写一个利用Eigen数据类型的通用类。我已经遇到了将构造函数参数分配给类成员变量的问题。我的代码的简化版本是:
template <typename Derived>
class A
{
public:
Eigen::Matrix<Derived> M; // error C2976: too few template parameters
A(const Eigen::DenseBase<Derived> & V)
{
M = V.eval(); // I would want to snapshot the value of V.
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题是,现在M
应该是哪种数据类型?我尝试了多种选择,例如:
Eigen::internal::plain_matrix_type_column_major<Derived> M;
Eigen::DenseBase<Derived> M;
Run Code Online (Sandbox Code Playgroud)
但是它们只会产生不同的错误。请注意,我使用C ++ 17并期望从构造函数中推断类模板参数。
对于这样的一个小例子,我只想接受T
if T
is astruct/class
并拒绝内置类型,如“int”、“char”、“bool”等。
template<typename T>
struct MyStruct
{
T t;
};
Run Code Online (Sandbox Code Playgroud) 为了避免代码重复,我需要做这样的事情(在我的真实代码中,我有更复杂的类型,类似于T1
和T2
):
template <class T1, class T2>
struct A
{};
template <class T1, class T2>
struct B
{};
template <class X>
struct C
{
using p1 = int;
using p2 = char;
using some = X<p1, p2>;
};
int main()
{
C<A> o1; // must produce C<A<int,char> >
C<B> o2; // must produce C<B<int,char> >
}
Run Code Online (Sandbox Code Playgroud) 在 Visual Studio 上编译如下:
\ntemplate<typename ArgType, typename ReturnType>\nstruct Test\n{\n using FunctionPointerType = std::conditional_t<\n std::is_same_v<ArgType, void>\n , ReturnType(*)()\n , ReturnType(*)(ArgType)\n >;\n FunctionPointerType Func;\n};\n\nint main()\n{\n Test<void, char> tt;\n}\n
Run Code Online (Sandbox Code Playgroud)\n但不能在 Linux g++ 上编译。我得到的错误是
\nerror : invalid parameter type \xe2\x80\x98void\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n我知道我不能在模板中使用 void,这就是我使用std::conditional_t
和 的原因std::is_same_v
。
我看不出什么是不正确的,有人可以告诉我吗?
\n我了解了 SFINAE 原理及其各种用途。然后我编写了以下程序,该程序使用 gcc 编译,但不使用 msvc 和 clang 编译。现场演示。
#include <iostream>
#include <type_traits>
template <typename T> class Container {
public:
template<typename U = T>
std::enable_if_t<std::is_same_v<T, int>> foo(const T&)
{
}
};
template<typename T>
void func(T&& callable)
{
Container<int> c;
(c.*callable)(4);
}
int main(){
//works with gcc but not with clang and msvc
func(&Container<int>::foo);
}
Run Code Online (Sandbox Code Playgroud)
正如我们所看到的,上面的程序适用于 gcc,但不适用于 clang 和 msvc,而且我不知道这里是哪个编译器。这个程序是格式良好还是格式错误等。
是否可以像在C++中一样在PHP中创建类模板?PHP可能没有类似的语言结构(如C++中的关键字),但也许有一些巧妙的技巧来实现类似的功能?template
我有一个Point
类想要转换为模板。在类中,我使用类型参数,因此,对于每个类,我想传递给 Point 方法,我必须使用适当类型的参数创建 Point 类的新副本。
这是C++ 的示例形式:
#include<iostream>
template <typename T>
class Point
{
public:
T x, y;
Point(T argX, T argY)
{
x = argX;
y = argY;
}
};
int main() {
Point<int> objA(1, 2);
std::cout << objA.x << ":" << objA.y << std::endl;
Point<unsigned> objB(3, 4);
std::cout << objB.x << ":" << objB.y << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在 PHP 中也是如此,但它根本不起作用(当然最后一行返回错误):
class SomeClass
{ …
Run Code Online (Sandbox Code Playgroud) 考虑这个代码:
template <typename T>
class Singleton
{
};
class Logger : public Singleton<Logger> {
friend class Singleton;
};
Run Code Online (Sandbox Code Playgroud)
它在 gcc 和 clang 中编译,但它有效吗?[temp.local].1 说:
当它与模板参数列表一起使用时,作为模板模板参数的模板参数,或作为朋友类模板声明的详细类型说明符中的最终标识符,它是一个模板名称指的是类模板本身。
粗体部分似乎适用,并且朋友声明似乎需要类型名称而不是模板名称(参见 [class.friend])。
编译器错了还是我误读了标准?
c++ friend-class language-lawyer class-template injected-class-name
c++ ×10
class-template ×10
templates ×5
c++17 ×3
c++11 ×2
arrays ×1
c++14 ×1
class ×1
eigen ×1
friend-class ×1
overloading ×1
php ×1