English 中文(简体)
"Null logger" - 与流运操作员一起的空舱会是空的吗? 这些操作员不会从优化代码中编集出来吗?
原标题:"null logger" - will empty class with ostream operator that does nothing get compiled out of optimised code?

如果我有一个伐木类,在释放模式下,是空的, 并且有一个流运算符, 毫无作用。 或多或少看起来是这样的:

struct null_logger
{
    template<typename T> inline null_logger& operator<<(T) { return *this; }
};

我创造了一个简单的测试 并粘贴了以下的组装:

const char* foo()
{
    return "hello";
}

int main()
{
    int i = 0;
    null_logger() << i << foo() << " this is a test";
    return 0;
}

坦率地说,我并不完全理解集会。 根据 @Als的建议,我找了 call 声明,但没有这些声明。 因此,是否可以假定,在发布模式下,任何致这个流操作员的电话都会被汇编出来?

这是生成的组件, 使用 g+++ -O3 - S main. cpp

    .file   "main.cpp"
    .section    .rodata.str1.1,"aMS",@progbits,1
.LC0:
    .string "hello"
    .text
    .p2align 4,,15
.globl _Z3foov
    .type   _Z3foov, @function
_Z3foov:
.LFB3:
    movl    $.LC0, %eax
    ret
.LFE3:
    .size   _Z3foov, .-_Z3foov
    .p2align 4,,15
.globl main
    .type   main, @function
main:
.LFB4:
    xorl    %eax, %eax
    ret
.LFE4:
    .size   main, .-main
    .section    .eh_frame,"a",@progbits
.Lframe1:
    .long   .LECIE1-.LSCIE1
.LSCIE1:
    .long   0x0
    .byte   0x1
.globl __gxx_personality_v0
    .string "zPR"
    .uleb128 0x1
    .sleb128 -8
    .byte   0x10
    .uleb128 0x6
    .byte   0x3
    .long   __gxx_personality_v0
    .byte   0x3
    .byte   0xc
    .uleb128 0x7
    .uleb128 0x8
    .byte   0x90
    .uleb128 0x1
    .align 8
.LECIE1:
.LSFDE1:
    .long   .LEFDE1-.LASFDE1
.LASFDE1:
    .long   .LASFDE1-.Lframe1
    .long   .LFB3
    .long   .LFE3-.LFB3
    .uleb128 0x0
    .align 8
.LEFDE1:
.LSFDE3:
    .long   .LEFDE3-.LASFDE3
.LASFDE3:
    .long   .LASFDE3-.Lframe1
    .long   .LFB4
    .long   .LFE4-.LFB4
    .uleb128 0x0
    .align 8
.LEFDE3:
    .ident  "GCC: (SUSE Linux) 4.3.4 [gcc-4_3-branch revision 152973]"
    .section    .comment.SUSE.OPTs,"MS",@progbits,1
    .string "Ospwg"
    .section    .note.GNU-stack,"",@progbits
最佳回答

并非直接回答您的问题,但通常您会以另一种方式禁用伐木:您只是不通过短路短路评估 Logo_DBG 之后的内容,因为短路很简单:

#ifdef NDEBUG
#define LOG_CHECK false &&
#elseif
#define LOG_CHECK /*empty*/

#define LOG_DBG LOG_CHECK /*your_debug_logging_code*/

这样, 您就能保证 < code> Logo_ DBG 之后的一切代码都是死代码, 可以在释放模式下优化 。

请注意,即使它们没有编成,由于短路,它们也不会在运行时被处决。

现在,要让它实际发挥作用, /* your_debug_bob_cob_code*/ 需要评估布林值,例如通过安全布尔的单词。 IO 流可以这样做, 以便让您知道流的状态是否仍然正常 。

class Logger{
  typedef void (Logger::*safe_bool)();
  void safe_bool_check(){}
public:
  // ...

  operator safe_bool() const{
    return check_state() ? &Logger::safe_bool_check : 0;
  }
};

当然,如果您只是希望布林转换使短路路工作,您可以在转换操作器中简单 return 0;

请注意,如果您正在使用一个真正的最新版本的海合会或Clang,您可能已经可以使用 explicate 转换操作员, < a href=>“https://stackoverflow.com/q/6242768/500104>,因此不需要安全布尔idom :

explicit operator bool() const{ return check_state(); }

另一种方法可能是使用长期操作员进行短路电路操作。 然而,两个“ 空隙” 都需要相同类型, 所以我们在这里会使用一个小把戏, 一个可以用任何东西构建的简单分类 :

namespace log_detail{
struct log_check_helper{
  log_check_helper(){}
  template<class T>
  log_check_helper(T const&){}
};
}

#ifdef NDEBUG
#define LOG_CHECK true ? log_detail::log_check_helper() : 
#else
#define LOG_CHECK /*empty*/
#endif

#define LOG_DBG LOG_CHECK /*your_debug_logging_code*/

在Ideone < /a > 上,用一个汇编和不评价IOSl流代码

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签