请考虑以下代码:
template <int dim>
struct vec
{
vec normalize();
};
template <>
struct vec<3>
{
vec cross_product(const vec& second);
vec normalize();
};
template <int dim>
vec<dim> vec<dim>::normalize()
{
// code to normalize vector here
return *this;
}
int main()
{
vec<3> direction;
direction.normalize();
}
Run Code Online (Sandbox Code Playgroud)
编译此代码会产生以下错误:
1> main.obj:错误LNK2019:未解析的外部符号"public:struct vec <3> __thiscall vec <3> :: normalize(void)"(?normalize @?$ vec @ $ 02 @@ QAE?AU1 @ XZ)引用在函数_main中
请考虑以下文件:
foo.h中
template <typename T>
struct Foo
{
int foo();
};
template <typename T>
int Foo<T>::foo()
{
return 6;
}
Run Code Online (Sandbox Code Playgroud)
foo.c的
#include "Foo.H"
template <>
int Foo<int>::foo()
{
return 7;
}
Run Code Online (Sandbox Code Playgroud)
MAIN.C
#include <iostream>
#include "Foo.H"
using namespace std;
int main()
{
Foo<int> f;
cout << f.foo() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译并运行时,打印7.这里发生了什么?什么时候模板实例化?如果编译器这样做,编译器如何知道不实例化它自己的Foo版本?
你好!有人知道实现或模仿以下行为的方法吗? (此代码导致编译时错误).
例如,我想仅在派生类中添加特定的模板特化.
struct Base {
template <typename T> void Method(T a) {
T b;
}
template <> void Method<int>(int a) {
float c;
}
};
struct Derived : public Base {
template <> void Method<float>(float a) {
float x;
}
};
Run Code Online (Sandbox Code Playgroud) 对于主要模板:
template<typename A, typename B> class MyClass {...
Run Code Online (Sandbox Code Playgroud)
与模板专业化,有什么区别
template<typename A, typename B> class MyClass<int, float> {...
Run Code Online (Sandbox Code Playgroud)
和
template<> class MyClass<int, float> {...
Run Code Online (Sandbox Code Playgroud) c++ templates metaprogramming partial-specialization specialization
我有一个关于C++模板专业化的问题,我希望有人可以提供帮助.我有一个有3个模板参数的类:
template<class A, class B, class C>
class myClass {
public:
void myFunc();
};
Run Code Online (Sandbox Code Playgroud)
我想要做的是写几个版本的myFunc,专门用于比如C,但是对于类型A和B是通用的.所以我不想要这样的完全模板化的函数:
template<class A, class B, class C>
void myClass<A, B, C>::myFunc()
{
// function code here
}
Run Code Online (Sandbox Code Playgroud)
我不想要像这样的完全专业化的功能
void myClass<int, int, int>::myFunc()
{
// code goes here
}
Run Code Online (Sandbox Code Playgroud)
相反,我想做一些类似的事情
template<class A, class B>
void myClass<A, B, int>::myFunc()
{
// code goes here
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,如果类类型C是int,我会调用myFunc()的一个版本,如果类类型C是double,我会调用不同版本的myFunc.我已经尝试了很多模板特化语法的差异组合(这里列出的太多了),似乎没有编译.
有人可能会指出我在正确的方向吗?在此先感谢您的帮助.
迈克尔
以下代码(这是我需要的简化版本)没有链接
在*.h文件中:
class InterfaceFunctionField2 {
public:
template<class outputType> outputType to() { return outputType(); }
};
Run Code Online (Sandbox Code Playgroud)
在*.cpp文件中
template<> double InterfaceFunctionField2::to<double>()
{ return 3.; }
Run Code Online (Sandbox Code Playgroud)
该类位于静态库中.
我收到"错误LNK2005:"public:double __thiscall InterfaceFunctionField2 :: to(void)const"(?? $ to @ N @ InterfaceFunctionField2 @@ QBENXZ)已在...中定义"和"第二个定义被忽略"警告LNK4006
我只定义了InterfaceFunctionField2 :: to()特化一次,我没有#include*.cpp文件....
我已经在互联网上查了一下(例如这里),这种类型的代码似乎没问题,但链接器不同意.你能帮忙吗?谢谢.
我只有一个简单的问题,请查看此代码:
template < typename A >
void foo( A a )
{ cout<<"1\n"; };
template< >
void foo<float>( float a )
{ cout<<"2\n"; }
void foo( float a )
{ cout<<"3\n"; }
int main()
{
foo<float>( 1.0f );
}
Run Code Online (Sandbox Code Playgroud)
用g ++ 4.7.2编译当然可以,但我不清楚的是为什么输出是"2"而不是"3".
据我记得,非模板函数应始终优先用于模板,因此为什么称为专用foo?
谢谢
虽然尝试编写一个包装器shared_ptr会在支持继承类的同时隐藏用户的内存分配和释放,但我偶然发现了一些非常奇怪的错误,这些错误表明编译器在重载期间查找错误的函数,或者我对混合重载和模板的了解是错误.所以我写了这个东西用于测试:
#include <iostream>
void out(int i) {
std::cout << i << '\n';
}
template <class T>
struct Inst {
template <class TT>
Inst(const TT &) {out(1);}
Inst(const Inst &) {out(2);}
template <class TT>
Inst(TT &&) {out(3);}
Inst(Inst &&) {out(4);}
Inst() {out(-1);}
~Inst() {out(1000);}
};
class K {};
class KK : K {};
int main() {
out(3000);
K k; KK kk; Inst<K> i;
Inst<K> I1{k};
Inst<K> I2{kk};
Inst<K> I3{i};
Inst<K> I4{K()};
Inst<K> I5{KK()};
Inst<K> I6{Inst<K>()};
out(2000);
}
Run Code Online (Sandbox Code Playgroud)
我会合理地期望会I1 …
我想专门化/子类化请求包,以添加一些具有自定义功能的方法。
我试图这样做:
# concrete_requests.py
import requests
class concreteRequests(requests):
def __init__(self):
super(concreteRequests, self).__init__()
self.session()
def login(self):
payload = {'user': 'foo', 'pass': 'bar'}
self.get('loginUrl', headers=header, data=payload)
# more login stuff...
# my_class.py
class MyClass:
def __init__():
self.requests = concreteRequests()
self.requests.login()
Run Code Online (Sandbox Code Playgroud)
这样,我仍然可以从self.requests成员和我的具体实现中受益。所以,我可以这样做:self.requests.get(...)或print(self.requests.post(...).status_code)等等。
我想这行super(concreteRequests, self).__init__()可能是愚蠢的无用,因为请求只是在导入中没有任何类声明。
因此,可以通过继承对请求包进行子类化/专门化吗?
我想要为其参数类型优化一个函数的多个版本,并根据上下文调用适当的函数.
在我的例子中,所有参数都具有相同的类型,并且都是等价的,因此它宁可避免使用self参数.
我试过这段代码:
trait Foo<T> {
fn foo(a: T, b: T, c: T);
}
impl Foo<i32> {
fn foo(a: i32, b: i32, c: i32) {}
}
impl Foo<i16> {
fn foo(a: i16, b: i16, c: i16) {}
}
fn main() {
Foo::foo(1i32,2,3);
Foo::foo(1i16,2,3);
}
Run Code Online (Sandbox Code Playgroud)
但Rust需要类型注释:
错误:需要输入注释:无法解析
_ : Foo<i32>[E0283]
我可以避免在呼叫站点提供类型注释吗?如果必须,怎么办?
specialization ×10
c++ ×8
templates ×7
hyperlink ×1
inheritance ×1
methods ×1
oop ×1
python ×1
rust ×1
traits ×1