gpt4 book ai didi

C++ 模板函数 : explicit instantiate one or more specializations

转载 作者:行者123 更新时间:2023-11-28 07:33:30 32 4
gpt4 key购买 nike

是否可以显式实例化模板函数的一个或多个特化?其次,函数是否是类成员有关系吗?它是合法的 C++11 吗?它是否被编译器接受,因此不会出现问题?

最佳答案

Is it possible to explicit instantiate one or more specializations of a template function?

是的,但是,[temp.explicit]/5:

For a given set of template arguments, if an explicit instantiation of a template appears after a declaration of an explicit specialization for that template, the explicit instantiation has no effect.


Second, does it matter if the function is a class member?

不,据我所知;[temp.explicit]/1:

A class, a function or member template specialization can be explicitly instantiated from its template. A member function, member class or static data member of a class template can be explicitly instantiated from the member definition associated with its class template. An explicit instantiation of a function template or member function of a class template shall not use the inline or constexpr specifiers.

来自 [temp.explicit]/3 的示例:

template<class T> class Array { void mf(); };
template class Array<char>;

template void Array<int>::mf();

template<class T> void sort(Array<T>& v) { /∗ ... ∗/ }
template void sort(Array<char>&); // argument is deduced here

namespace N {
template<class T> void f(T&) { }
}
template void N::f<int>(int&);

Is it legal C++11 and also is it accepted by compilers so it doesn't come with problems?

嗯,是的,但是对于库来说,总是存在 ABI 兼容性问题;特别是如果不同的编译器已用于库和库用户(例如,包含该库的程序)。 C++ 标准不指定 ABI。

关于C++ 模板函数 : explicit instantiate one or more specializations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17195773/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com