有没有办法创建一个类型A:
鉴于:
A f(...);
然后:
双方auto&& a = f(...);并const auto& a = f(...);给出一个编译错误?
这样做的原因是,在这种情况下,A是一个表达式模板,其中包含对temporaries(作为参数提供f)的引用,因此我不希望此对象的生命周期超出当前表达式.
注意我可以auto a = f(...);通过将As复制构造函数设为私有来防止成为问题,并f(...)在需要时成为A的朋友.
代码示例 (ideone链接):
#include <iostream>
#include <array>
template <class T, std::size_t N>
class AddMathVectors;
template <class T, std::size_t N>
class MathVector
{
public:
MathVector() {}
MathVector(const MathVector& x)
{
std::cout << "Copying" << std::endl;
for (std::size_t i = 0; i != N; ++i)
{
data[i] = …Run Code Online (Sandbox Code Playgroud) 我可以想象表达模板对于像向量/矩阵/四元数等普遍存在的事物的编译时间做了很多事情,但如果它是如此大的速度提升为什么游戏不使用呢?很明显,SIMD指令可以利用数据级并行性来产生很好的效果.表达模板和懒惰的评估似乎有意义,至少在消除临时性时.
因此,虽然像Eigen这样的图书馆会宣传这些功能,但我并不认为这在中间件(例如Havok)或事物速度极其重要的游戏中都会发生.任何人都可以对此有所了解吗?它与非确定性或分支预测有关吗?
我正在构建一个使用表达式模板的库,我在类中大量使用模板化函数.我的所有代码都在运行,最近我决定将主类设置为允许在不同类型的数据上使用它.但是,我再也无法专注于我的功能了.我该如何解决这个问题?我附上了一个显示问题的小测试程序.
我以前的Animal课程没有模板化,然后这段代码工作正常.
#include<iostream>
#include<vector>
// Example templated class with templated function
template<class T>
class Animals
{
public:
template<class X>
void printFood(const X& x) const { std::cout << "We eat " << x << "!" << std::endl; }
private:
std::vector<T> animals;
};
// How do I specialize?
template<class T> template<>
void Animals<T>::printFood(const unsigned int& x) const { std::cout << "We don't want to eat " << x << "!" << std::endl; }
// Main loop;
int main()
{
Animals<double> …Run Code Online (Sandbox Code Playgroud) 使用以下代码时,不显示"hello2",因为在第3行上创建的临时字符串在执行第4行之前死亡.在第1行使用#define避免了这个问题,但有没有办法在不使用#define的情况下避免这个问题?(C++ 11代码没问题)
#include <iostream>
#include <string>
class C
{
public:
C(const std::string& p_s) : s(p_s) {}
const std::string& s;
};
int main()
{
#define x1 C(std::string("hello1")) // Line 1
std::cout << x1.s << std::endl; // Line 2
const C& x2 = C(std::string("hello2")); // Line 3
std::cout << x2.s << std::endl; // Line 4
}
Run Code Online (Sandbox Code Playgroud)
澄清:
请注意,我相信Boost uBLAS存储引用,这就是为什么我不想存储副本.如果您建议我按值存储,请解释为什么Boost uBLAS出错并且按值存储不会影响性能.
我在编译器相关问题中遇到以下代码(存储在crtp.cc中):
#include <vector>
#include <cassert>
#include <iostream>
template < class Derived >
class AlgebraicVectorExpression {
public:
typedef std::vector<double>::size_type SizeType;
typedef std::vector<double>::value_type ValueType;
typedef std::vector<double>::reference ReferenceType;
SizeType size() const {
return static_cast<const Derived&>(*this).size();
}
ValueType operator[](SizeType ii) const {
return static_cast<const Derived&>(*this)[ii];
}
operator Derived&() {
return static_cast<Derived&>(*this);
}
operator const Derived&() const {
return static_cast< const Derived& >(*this);
}
};
template< class T1, class T2>
class AlgebraicVectorSum : public AlgebraicVectorExpression< AlgebraicVectorSum<T1,T2> > {
const T1 & a_;
const T2 & …Run Code Online (Sandbox Code Playgroud) 我如何连接两个const_string?它的主页http://conststring.sourceforge.net/表示引用:
它还使用表达式模板进行连接,有效地消除了创建中间临时对象所产生的开销.
所以我认为它是最简单的形式
typedef class boost::const_string<char> csc;
csc a, b;
csc c = a+b;
Run Code Online (Sandbox Code Playgroud)
应该管用.但gcc-4.6抱怨运营商+.为什么?我一定要投a,并b到std::string?
错误输出如下:
semnet/realfile.cpp:185:13: error: no match for ‘operator+’ in ‘a + b’
semnet/realfile.cpp:185:13: note: candidates are:
/usr/include/c++/4.6/bits/stl_bvector.h:352:3: note: std::_Bit_const_iterator std::operator+(std::ptrdiff_t, const std::_Bit_const_iterator&)
/usr/include/c++/4.6/bits/stl_bvector.h:352:3: note: no known conversion for argument 1 from ‘csc’ to ‘std::ptrdiff_t’
/usr/include/c++/4.6/bits/stl_bvector.h:266:3: note: std::_Bit_iterator std::operator+(std::ptrdiff_t, const std::_Bit_iterator&)
/usr/include/c++/4.6/bits/stl_bvector.h:266:3: note: no known conversion for argument 1 from ‘csc’ to ‘std::ptrdiff_t’
/usr/include/c++/4.6/bits/basic_string.h:2414:20: note: template<class _CharT, …Run Code Online (Sandbox Code Playgroud) c++ boost string-concatenation immutability expression-templates