鉴于:
struct A
{
virtual bool what() = 0;
};
template<typename T, typename Q>
struct B : public A
{
virtual bool what();
};
Run Code Online (Sandbox Code Playgroud)
我想部分专注what
如下:
template<typename T, typename Q>
bool B<T, Q>::what()
{
return true;
}
template<typename Q>
bool B<float, Q>::what()
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
但似乎这是不可能的(它是否在C++ 11中?)所以我尝试了SFINAE:
template<typename T>
typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what()
{
return true;
}
template<typename T>
typename std::enable_if<!std::is_same<T, float>::value, bool>::type B<T>::what()
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
这也行不通,我不知道为什么,有人吗?所以我找到了这个帖子,结果是:
template<typename T, typename Q>
struct B …
Run Code Online (Sandbox Code Playgroud) 多个类模板特化是否有效,当每个模板特化仅在涉及非推导上下文中的模板参数的模式之间不同时?
一个常见的例子是std::void_t
使用它来定义一个特征,它揭示了一个类型是否有一个typedef
名为"type" 的成员.这里采用单一专业化.这可以扩展到鉴定说类型是否有任何成员typedef
被称为"TYPE1",或者一个名为"2型".下面的C++ 1z代码用GCC编译,但不是Clang.这合法吗?
template <class, class = std::void_t<>>
struct has_members : std::false_type {};
template <class T>
struct has_members<T, std::void_t<typename T::type1>> : std::true_type {};
template <class T>
struct has_members<T, std::void_t<typename T::type2>> : std::true_type {};
Run Code Online (Sandbox Code Playgroud) c++ partial-specialization sfinae template-specialization c++17
以下模板专业化代码:
template<typename T1, typename T2>
void spec1()
{
}
Run Code Online (Sandbox Code Playgroud)
测试案例1:
template< typename T1> //compile error
void spec1<int>()
{
}
Run Code Online (Sandbox Code Playgroud)
测试案例2:
template< typename T2> //compile error
void spec1<int>()
{
}
Run Code Online (Sandbox Code Playgroud)
生成以下编译错误:
错误C2768:'spec1':非法使用显式模板参数
有谁知道为什么?
struct Bar {
enum { Special = 4 };
};
template<class T, int K> struct Foo {};
template<class T> struct Foo<T,T::Special> {};
Run Code Online (Sandbox Code Playgroud)
用法:
Foo<Bar> aa;
Run Code Online (Sandbox Code Playgroud)
无法使用gcc编译4.1.2它抱怨使用T::Special
Foo的部分特性.如果Special
是一个类,解决方案将是前面的类型名称.枚举(或整数)是否有相当于它的东西?
注意:这似乎是一个问题的转贴:C++ - 重载模板化的类方法,并对该方法进行了部分说明
我已经将C++模板专业化的问题归结为一个简单的案例.
它由一个简单的2参数模板类组成Thing
,我想专门Thing<A,B>::doSomething()
研究它B=int
.
#include <cstdio>
//
// A 3-parameter template class.
//
template <class A, class B>
class Thing
{
public:
Thing(A a, B b) : a_(a), b_(b) {}
B doSomething();
private:
A a_;
B b_;
};
//
// The generic case works as expected.
//
template <class A, class B>
B Thing<A,B>::doSomething()
{
return b_;
}
//
// This specialization does not work!
//
template <class A>
int Thing<A,int>::doSomething() …
Run Code Online (Sandbox Code Playgroud) 这是理解转换运算符,模板和模板特化的主要学术练习.在下面的代码转换操作符模板工程int
,float
以及double
,但使用时失败std::string
...之类的.我已经创建了转换为的特化std::string
,它在初始化时使用std::string s = a;
,但在与强制转换一起使用时失败static_cast<std::string>(a)
.
#include <iostream>
#include <string>
#include <sstream>
class MyClass {
int y;
public:
MyClass(int v) : y(v) {}
template <typename T>
operator T() { return y; };
};
template<>
MyClass::operator std::string() {
std::stringstream ss;
ss << y << " bottles of beer.";
return ss.str();
}
int main () {
MyClass a(99);
int i = a;
float f = a;
double d = a; …
Run Code Online (Sandbox Code Playgroud) ##A.hh
template<class T> void func(T t) {}
template<> void func<int>(int t) {}
void func2();
##A.cpp
void func2() {}
##main.cpp
func("hello");
func(int());
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:错误LNK2005:"void __cdecl func(int)"(?? $ func @ H @@ YAXH @ Z)已在A.obj中定义,找到一个或多个多重定义的符号
函数模板特化是否不被视为普通函数模板?看起来它将在A的目标文件中.
template <size_t size, typename ...Params>
void doStuff(Params...) {
}
template <>
void doStuff<size_t(1), int, bool>(int, bool) {
}
int main(int, char**) {
doStuff<1,int,bool>(1, false);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不会编译,第二个doStuff声明给了我,error: template-id ‘doStuff<1u, int, bool>’ for ‘void doStuff(int, bool)’ does not match any template declaration
但它明确地将第一个声明与variadic模板参数匹配.
如何专门研究可变参数模板?
c++ templates template-specialization variadic-templates c++11
在尝试使用最近的g ++ - 5编译器时,我在一个文件中写下了以下语句:
template<T> T a;
template<> int a = 1;
Run Code Online (Sandbox Code Playgroud)
结果如下:
警告:模板标题太多
a
(应为0)
同样有效的是,它并不专门a<int>
.例如
template<typename T> T a;
template<> int a = 1;
int main () {
std::cout << a<double> << "\n"; // prints 0; OK
std::cout << a<int> << "\n"; // prints 0! why not 1?
}
Run Code Online (Sandbox Code Playgroud)
这句法的神秘之处是什么?
c++ template-specialization language-lawyer variable-templates c++14
// #1
template <typename T, T n1, T n2>
struct foo {
static const char* scenario() {
return "#1 the base template";
}
};
// #2
// partial specialization where T is unknown and n1 == n2
template <typename T, T a>
struct foo<T, a, a> {
static const char* scenario() {
return "#2 partial specialization";
}
};
Run Code Online (Sandbox Code Playgroud)
下面的主要内容在g ++(6.1)和clang ++(3.8.0)上有不同的结果:
extern const char HELLO[] = "hello";
double d = 2.3;
int main() {
cout << foo<int, 1, 2> …
Run Code Online (Sandbox Code Playgroud) c++ templates partial-specialization template-specialization