我的问题是:
C++ dynamic_cast vs在静态枚举中存储对象类型?
这个问题没有得到答复.dynamic_cast需要RTTI,而虚函数需要表查找,减慢他们的调用(我知道Stroustrup推荐这个).枚举+访问器是识别类型之间最快的方法吗?
编辑:
我的帖子的重点是一堂课Screen:
class ScreenImpl;
class Screen
{
public:
enum Type
{
GLES1,
GLES2,
GLES3
};
enum Type type() const noexcept { return type_; }
private:
enum Type type_;
ScreenImpl* impl_;
};
Run Code Online (Sandbox Code Playgroud)
该类可以有不同的实现(使用PIMPL,可以创建3个不同的上下文之一,头文件保持不变,所以a static_cast是可以的)我认为对象可能在它们正在运行的上下文(GLES1,GLES2或GLES3)下查询.或者,我可以使用a dynamic_cast(至少需要1个虚拟成员函数)或typeid.现在,在阅读帖子后,我想我将废除这一点并让所有对象知道它们在什么上下文中提前运行(前面所有的ifs和switch,以及虚函数的调用).
在使用关联容器时,我观察到,至少有时是一系列的:
container[key].field1 = something1;
container[key].field2 = something2;
container[key].field3 = something3;
Run Code Online (Sandbox Code Playgroud)
产生比二进制(可执行)更小的二进制文件
auto& c(container[key]);
c.field1 = something1;
c.field2 = something2;
c.field3 = something3;
Run Code Online (Sandbox Code Playgroud)
我很迷惑.AFAIK,每个container[key] = ...语句都需要查找一个值.编译器是否优化了这些查找?什么是最好的事情?
我遇到类型有困难,例如:
T******************************************
Run Code Online (Sandbox Code Playgroud)
应该std::remove_pointer能够处理这种类型(我不这么认为)?如何删除所有*?如果&最后添加了一个或两个&符号,那么是否存在C++11标准的元函数将这种类型剥离到T不使用std::remove_reference和std::remove_pointer元函数?
说我有这种类型:
template <typename T, typename U>
using product_type = decltype(::std::declval<T>() * ::std::declval<U>());
Run Code Online (Sandbox Code Playgroud)
我在函数模板中使用
template <typename T, typename U>
product_type<T, U> product(T const a, U const b)
{
return a * b;
}
Run Code Online (Sandbox Code Playgroud)
模板产生的模板函数是否会返回C++基本类型的"合理"产品值?我想这将使用C++类型的促销规则.是否有更好,更正确的方法来返回"合理"基本类型的值?我担心我可能会回复一个float产品,例如a double和a float.
如您所知,前两个是特定于 AVX 的内在函数,第二个是 SSE4.1 内在函数。两组内在函数都可用于检查 2 个浮点向量的相等性。我的具体用例是:
_mm_cmpeq_ps或_mm_cmpeq_pd,然后是_mm_testc_ps或_mm_testc_pd在结果上,使用适当的掩码但是 AVX 为“传统”内在函数提供了等价物,因此_mm_testc_si128在将结果转换为__m128i. 我的问题是,这两个用例中的哪一个会导致更好的性能,以及我在哪里可以找到 AVX 提供了哪些旧的 SSE 指令。
我有:
T* p;
Run Code Online (Sandbox Code Playgroud)
而且我想这样做:
p->~decltype(*p)();
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用.
p->~T();
Run Code Online (Sandbox Code Playgroud)
确实有效,但decltype()如果可能的话,它喜欢这样做.
错误信息:
error: expected class-name before 'decltype'
Run Code Online (Sandbox Code Playgroud)
编辑:用例
我正在尝试使用数组的alloca + placement new来避免在堆栈上分配的数组使用VLA.放置new之后,需要显式销毁堆栈数组中的对象.我知道如何解决问题,我可以这样做:
using T = ::std::remove_reference_t<decltype(*p)>;
p->~T();
Run Code Online (Sandbox Code Playgroud)
但是,我很困惑为什么这是必要的.
例如:
int main()
{
int p;
::std::cout << ::std::uintptr_t(&p) << ::std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如果重复执行,这将始终产生“随机”数字。可以在 C 中完成类似的事情。我看不到任何关于它的 UB。
拿这个功能模板:
template <typename T>
T divby2(T a)
{
return .5 * a;
}
Run Code Online (Sandbox Code Playgroud)
是否存在一种指定(.5),一个double恒定的,所以它不会在运行时被转换成T时T != double(比如,当T是float)?
关于.5常数的替代规范的一些想法:
template <typename T>
T divby2(T a)
{
return T(.5) * a;
}
template <typename T>
T divby2(T a)
{
return (T(1) / T(2)) * a;
}
Run Code Online (Sandbox Code Playgroud) 我有这个想法在.cpp文件中使用:
namespace
{
bool operator==(char const* const a, char const* const b) noexcept
{
return !::std::strcmp(a, b);
}
}
Run Code Online (Sandbox Code Playgroud)
这是好风格吗?
编辑:
我认为有条件的c ++ 1z方式,即完成同样的事情,将使用新std::string_view类进行比较.
struct A
{
template <typename T>
constexpr explicit operator
std::enable_if_t<
std::is_same<std::decay_t<T>, int>{},
int
>() const noexcept
{
return -1;
}
};
int main()
{
A a;
std::cout << int(a) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
错误是clang-7.0.1:
<source>:21:16: error: no matching conversion for functional-style cast from 'A' to 'int'
std::cout << int(a) << std::endl;
^~~~~
<source>:7:22: note: candidate template ignored: couldn't infer template argument 'T'
constexpr explicit operator
^
Run Code Online (Sandbox Code Playgroud)