我在将一些东西从Solaris移植到Linux时遇到的一个问题是Solaris编译器__FILE__在预处理过程中将宏扩展为文件名(例如MyFile.cpp),而Linux上的gcc扩展到完整路径(例如/ home /用户/ MYFILE.CPP).使用basename()可以很容易地解决这个问题但是......如果你经常使用它,那么对basename()的所有调用都必须加起来,对吧?
这是问题所在.有没有办法使用模板和静态元编程,在编译时运行basename()或类似的?由于它__FILE__是常量且在编译时已知,因此可能更容易.你怎么看?可以吗?
我知道大多数运营商的名字,但不知道什么operator<<和operator>>被称为.
即
operator=() // the assignment operator
operator==() // the equality of comparison operator
operator++() // the increment operator
operator--() // decrement operator etc.
operator<() // the less-than operator
Run Code Online (Sandbox Code Playgroud)
等等......
我需要从给定起始行号和结束行号的文件中提取一定数量的行.
我怎么能在unix下快速执行此操作(实际上是Solaris,因此gnu风格不可用).
谢谢
我在Linux上使用读/写锁,我发现尝试将读锁定对象升级为写锁死锁.
即
// acquire the read lock in thread 1.
pthread_rwlock_rdlock( &lock );
// make a decision to upgrade the lock in threads 1.
pthread_rwlock_wrlock( &lock ); // this deadlocks as already hold read lock.
Run Code Online (Sandbox Code Playgroud)
我已经阅读了手册页,它非常具体.
如果在进行调用时调用线程可能会死锁,它会保持读写锁定(无论是读还是写锁).
在这些情况下,将读锁升级到写锁的最佳方法是什么.我不想在我保护的变量上引入竞争.
据推测,我可以创建另一个互斥锁来包含释放读锁定和获取写锁定但是我并没有真正看到使用读/写锁定.我不妨简单地使用普通的互斥锁.
谢谢
我偶然发现了以下代码,并想知道它的优点
std::string const & thestring( "XYZ" );
Run Code Online (Sandbox Code Playgroud)
事实上,它正在构建对象并通过引用引用它.我以前常常看到
std::string const theString( "XYZ" );
Run Code Online (Sandbox Code Playgroud)
并且想知道差异是什么.我很高兴对象不会被早期破坏,因为对象与引用一起存在于堆栈中.
鉴于此代码:
class X
{
public:
template< typename T >
void func( const T & v );
};
template<>
void X::func< int >( const int & v )
{
}
template<>
void X::func< char * >( const char * & v ) // 16
{
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时,我得到以下错误.
test.cpp:16: error: template-id 'func<char*>' for 'void X::func(const char*&)' does not match any template declaration
Run Code Online (Sandbox Code Playgroud)
任何人都可以对此有所了解吗?
我有一个大型的代码库,它构建了几十个库和几个可执行文件.
代码库按层次分解,并且几乎每个级别都构建了库.
我已经完成并在每个目录中放置了一个CMakeLists.txt文件来构建每个库.
在每个CMakeLists.txt中,我使用了"project(xxx)"指令.这为我定义了PROJECT_NAME,PROJECT_SOURCE_DIR和PROJECT_BINARY_DIR变量,我明智地使用了这些变量.
然而,其中一个团队对这种方法不满意,因为他找不到任何其他人做过这个的真实世界的例子.他经常引用KitWare示例不使用这种方法,因此我们也不应该这样做.
他提倡的替代方法是在每个makefile中设置这些变量,这看起来就像"项目"给你的那样.
我真的看不出他的观点,并且在说服他的方面没有取得什么进展.任何人都可以用这种方式阐明使用项目指令的缺点.
我抛弃你的集体智慧?
好的,这里有一些代码概述了我正在尝试做的事情.
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <iostream>
#include <sstream>
int main( int c, char *v[] )
{
int fd = open( "data.out", O_RDONLY | O_NONBLOCK );
std::cout << "fd = " << fd << std::endl;
char buffer[ 1024000 ];
ssize_t nread;
std::stringstream ss;
while( true )
{
if ( (nread = read( fd, buffer, sizeof( buffer ) - 1 )) < 0 )
break;
ss.write( buffer, nread );
while( true )
{
std::stringstream s2;
std::cout << "pre-get : …Run Code Online (Sandbox Code Playgroud) 我需要阻止一个类派生,所以我想,这是Boost必须已经完成的事情.我知道他们有一个不可复制的,他们必须有一个不可复制的......
想象一下,当我找不到它时,我感到惊讶......
这让我思考..必须有一个理由.也许不可能使用模板..
我确定它是否很容易在升级库中.
我知道如何在不使用模板的情况下完成它,即使用带有私有构造函数的基类,即
class ThatCantBeDerived; // Forward reference
class _NonDeriv
{
_NonDeriv() {}
friend class ThatCantBeDerived;
};
class ThatCantBeDerived : virtual public _NonDeriv
{
public:
ThatCantBeDerived() :
_NonDeriv()
{
}
};
Run Code Online (Sandbox Code Playgroud)
或类似的东西..
也许它是导致问题的前向参考,或者可能没有可移植的方法来实现它.
无论哪种方式,我不确定为什么它不是在提升..
有任何想法吗?
我有以下代码,不会编译,它是星期五,我有点疲惫.
#include <string>
#include <memory>
#include <utility>
#include <map>
template< typename T, typename ...Args >
std::unique_ptr< T > make_unique( Args && ...args )
{
return std::unique_ptr< T >( new T( std::forward< Args >( args )... ) );
}
struct A
{
};
std::map< std::string, std::unique_ptr< A > > _map = { { "A", make_unique< A >() } }; // <-- ERROR!!
Run Code Online (Sandbox Code Playgroud)
以下编译没有问题
int main()
{
std::pair< std::string, std::unique_ptr< A > > p { "B", make_unique< A >() };
_map.insert( std::make_pair( …Run Code Online (Sandbox Code Playgroud) c++ ×9
templates ×2
boost ×1
c ×1
cmake ×1
constructor ×1
dictionary ×1
locking ×1
operators ×1
pthreads ×1
reference ×1
shell ×1
stream ×1
unique-ptr ×1
unix ×1